delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/03/11:38:15

From: Remi Coulom <Remi DOT Coulom AT ensisun DOT imag DOT fr>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Static member functions in constructor
Date: Tue, 03 Jun 1997 14:33:36 +0200
Organization: IMAG, Grenoble, France
Message-ID: <33940F20.167E@ensimag.imag.fr>
References: <AAuNrmY0pst AT mmf DOT univ DOT simbirsk DOT su>
NNTP-Posting-Host: amoot.imag.fr
Mime-Version: 1.0
Lines: 67
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

A.V. Kondratiev wrote:
> 
> I encountered a strange g++ behavior.
> I tried to pass a static member function as a default parameter
> of constructor. Here is an example:
> 
> typedef void ( *ft )();
> 
> class A {
> public:
>   static void F1();
>   const ft pf;
>   A(ft f=F1):pf(f){}         // (1)
> //  A(ft f=0):pf(f ? f:F1){} // (2)
> };
> 
> void A::F1() {/*something*/}
> 
> int main() {
>   A a;
>   return 0;
> }
> 
> In case (1) the compiler reports that F1 is not defined. GCC doc
> tells that `g++' reports as undefined symbols any static data
> members that lack definitions. But I provided definition for F1!
> 
> I tried to work around this strange behavior as in case (2). It works
> but may anybody suggest a better solution? Of course, the easiest way
> is to declare F1 as a normal outer function, not as a static member.
> But, in my opinion, it is not a good solution in object-oriented
> programming.
> 
> Aleksey.

One solution would be :

<TT>
typedef void ( *ft )();

class A {
public:

  class B {
  public:
    static void F1();
  };

  const ft pf;
  A(ft f=B::F1):pf(f){}
};

void A::B::F1() {}

int main() {
  A a;
  return 0;
}
</TT>

 If you do not want to acces private static members of the A class
in the F1 function, then you could move class B outside of class A.

 I must admit this solution is not very elegant, but it does not have
the run-time overhead of <TT>f ? f:F1</TT>.

Remi

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019