public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: Error on Member Initialization
@ 2003-09-15 17:22 lrtaylor
  0 siblings, 0 replies; 5+ messages in thread
From: lrtaylor @ 2003-09-15 17:22 UTC (permalink / raw)
  To: lrtaylor, eljay, anw, gcc-help

Sorry, I just went back and look at his original code.  I see what he was doing wrong.  However, I'm still curious about my question...

Thanks,
Lyle Taylor
IS Applications

-----Original Message-----
From: lrtaylor 
Sent: Monday, September 15, 2003 11:21 AM
To: eljay@adobe.com; anw@csunv.com; gcc-help@gcc.gnu.org
Subject: RE: Error on Member Initialization

Eljay,

While that's the _preferred_ way to initialize member variables in C++, that's not always possible; sometimes you need to do more complex things to initialize your class (e.g., if you need to initialize variables differently based on certain conditions, retrieve information from a database, etc.).  Is there any _real_ difference between these two ways of initializing?

radApp::radApp()
: m_MyServer(&Log)
{}


radApp ::radApp()
{
  m_MyServer = &Log ;
}

Thanks,
Lyle Taylor
IS Applications

-----Original Message-----
From: Eljay Love-Jensen [mailto:eljay@adobe.com]
Sent: Saturday, September 13, 2003 8:28 AM
To: Allen Williams; gcc-help@gcc.gnu.org
Subject: Re: Error on Member Initialization

Hi Allen,

That's not how you initialize a member variable in C++.  (Although it's very close to how you do it in Java.)

You need to put the initialization in your radApp's constructor's initialization list.

e.g.:
radApp::radApp()
: m_MyServer(&Log)
{ }

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Error on Member Initialization
  2003-09-15 17:21 lrtaylor
@ 2003-09-15 17:40 ` John Love-Jensen
  0 siblings, 0 replies; 5+ messages in thread
From: John Love-Jensen @ 2003-09-15 17:40 UTC (permalink / raw)
  To: lrtaylor, gcc-help

Hi Lyle,

>Is there any _real_ difference between these two ways of initializing?

Yes.  The real difference is that the initializer list occurs whether or not
it is explicitly specified.  Putting the initialization code in the
constructor's body MAY incur a performance penalty.  That's a MAY, not a FOR
SURE ... the only way to know for certain is by profiling the optimized
release executable.

In some situations, it is necessary to put more complex resource acquisition
code in the constructor's body.

But, generally speaking, initialization lists are preferable over
initialization via the constructor's body.

The sample code you presented is amenable to the preferred initialization
list solution.

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Error on Member Initialization
@ 2003-09-15 17:21 lrtaylor
  2003-09-15 17:40 ` John Love-Jensen
  0 siblings, 1 reply; 5+ messages in thread
From: lrtaylor @ 2003-09-15 17:21 UTC (permalink / raw)
  To: eljay, anw, gcc-help

Eljay,

While that's the _preferred_ way to initialize member variables in C++, that's not always possible; sometimes you need to do more complex things to initialize your class (e.g., if you need to initialize variables differently based on certain conditions, retrieve information from a database, etc.).  Is there any _real_ difference between these two ways of initializing?

radApp::radApp()
: m_MyServer(&Log)
{}


radApp ::radApp()
{
  m_MyServer = &Log ;
}

Thanks,
Lyle Taylor
IS Applications

-----Original Message-----
From: Eljay Love-Jensen [mailto:eljay@adobe.com]
Sent: Saturday, September 13, 2003 8:28 AM
To: Allen Williams; gcc-help@gcc.gnu.org
Subject: Re: Error on Member Initialization

Hi Allen,

That's not how you initialize a member variable in C++.  (Although it's very close to how you do it in Java.)

You need to put the initialization in your radApp's constructor's initialization list.

e.g.:
radApp::radApp()
: m_MyServer(&Log)
{ }

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Error on Member Initialization
  2003-09-13  0:23 ` Allen Williams
@ 2003-09-13 14:28   ` Eljay Love-Jensen
  0 siblings, 0 replies; 5+ messages in thread
From: Eljay Love-Jensen @ 2003-09-13 14:28 UTC (permalink / raw)
  To: Allen Williams, gcc-help

Hi Allen,

That's not how you initialize a member variable in C++.  (Although it's very close to how you do it in Java.)

You need to put the initialization in your radApp's constructor's initialization list.

e.g.:
radApp::radApp()
: m_MyServer(&Log)
{ }

HTH,
--Eljay


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Error on Member Initialization
       [not found] <1062804110.11035.ezmlm@gcc.gnu.org>
@ 2003-09-13  0:23 ` Allen Williams
  2003-09-13 14:28   ` Eljay Love-Jensen
  0 siblings, 1 reply; 5+ messages in thread
From: Allen Williams @ 2003-09-13  0:23 UTC (permalink / raw)
  To: gcc-help

Hello,

I have the following declared:

class RADCOMMGT_API CradComMgt
    {
public:
    CradComMgt(CLog* Log=NULL);
    ~CradComMgt();

	--- snip ---
	};
/***************************************/

In another file I declare this:

extern CLog Log;                   // <- Note declaration of CLog object

class radApp : public lnDaemon
    {
  public:
    virtual void DaemonInit();

  protected:
    CradComMgt m_MyServer(&Log);	// <- Note initialization
    };
/******************************************/

I get this from the compiler:

g++ -o radApp -D_LINUX -L../Libraries -I../Headers
radApp.cpp -lApp -lLog -lSock -lNetDataSvc -lradComMgt -lradCore
In file included from radApp.cpp:16:
radApp.h:24: error: invalid data member initialization
radApp.h:24: error: (use `=' to initialize static data members)
make: *** [radApp] Error 1

Compilation exited abnormally with code 2 at Fri Sep 12 20:04:53

I have Stroustrup's C++ reference manual and Ellis and Stroustrup's C++
Annotated reference, both of which say this should be a valid form to call
the CradComMgt::CradComMgt(CLog *) constructor.  What am I doing wrong here?

Thanks in Advance,
anw

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-09-15 17:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-15 17:22 Error on Member Initialization lrtaylor
  -- strict thread matches above, loose matches on Subject: below --
2003-09-15 17:21 lrtaylor
2003-09-15 17:40 ` John Love-Jensen
     [not found] <1062804110.11035.ezmlm@gcc.gnu.org>
2003-09-13  0:23 ` Allen Williams
2003-09-13 14:28   ` Eljay Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).