Re: How to create an initialised object declared as a class member variable?

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 08/18/04


Date: Wed, 18 Aug 2004 13:28:11 +0200

CFF wrote:
>
> /////////////////////////// fileB.h ///////////////////////////
> //
> #include "fileA.h"
>
> class CMyClassB{
> public:
> CMyClassA m_B1; // no problem
> CMyClassA m_B2(this); // syntax error : 'this', WHY???

Because this is not the way initializations are specified in C++
What you need is a constructor for CMyClassB, which does the initialization

     CMyClassB() : m_B2( this ) {}

-- 
Karl Heinz Buchegger
kbuchegg@gascad.at


Relevant Pages

  • Re: How to create an initialised object declared as a class member variable?
    ... CFF wrote: ... > class CMyClassB{ ... Because this is not the way initializations are specified in C++ ... What you need is a constructor for CMyClassB, ...
    (comp.lang.cpp)
  • Re: What is going on here?
    ... >>declaration in the constructor function. ... Its use is to specify to the compiler what initializations should ... initializations of member variables or the initialization ... an empty declaration. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: slab quirks in DEBUG, ctor, and initialization
    ... for each successful allocation. ... cache is grown (see cache_grow calling cache_init_objs) which is the ... that your constructor is used only for newly allocated ... of it, and were the initializations performed into allocated space), ...
    (Linux-Kernel)
  • Re: Not Initializing Variables : Performance impact
    ... value you are setting - they lead to code bloat. ... write them into every constructor in your class (so 3x3 ... initializations in this case). ... significant performance issue? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: What is going on here?
    ... > declaration in the constructor function. ... initializations of member variables or the initialization ... of the base class this class is derived from. ... Lots of empty statements ...
    (alt.comp.lang.learn.c-cpp)