Calling a struct constructor in a class constructor body

From: Karl M (Karl.M_at_email.com)
Date: 12/18/04


Date: Sat, 18 Dec 2004 05:41:34 -0500

Hi C++ experts!

Definitely I skipped C/C++ 101 because I have this pitfall:

I need to call the struct ctor in the class default ctor body see below:

//Some.h

struct MyStruct

{

            int a[5];

            .

};

class MyClass

{

public:

            MyClass();

~MyClass();

            .

protected:

            .

            MyStruct m_MyStruct;

            .

};

//some.cpp

#include "some.h"

MyClass::MyClass() : m_MyStruct()

// default ctor of the struct with NULL- it works but not with the order of
class member declarations I have, so...

{

            // here I need to have m_MyStruct() initialized with NULL not
using m_MyStruct.a[1] = 0; etc .

            //How? Or maybe.m_MyStruct = MyStruct(); I this correct?

}

It's painful, now I understand VB programmers - easy street :)

Thank you,

Karl M



Relevant Pages

  • Re: C++ Compiler behavior regerding struct constructors
    ... C++ guarantees zero initialization for static variables of primitive type, ... MyStruct m_MyStruct = MyStruct; ... > struct MyStruct ... > MyClass::MyClass//The default ctor for the class does not init the ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Self Assignment check in Copy ctor
    ... > AlgoMan wrote: ... >> Do you need to provide a self assignment check in a copy ctor ...
    (comp.lang.cpp)
  • Re: defined in constructor
    ... "JSmith" wrote in message ... >> So this line only appears inside the ctor and the implementation of the ... >> is only in the cpp file? ... it should be global and please know that the struct is not defind ...
    (microsoft.public.vc.language)
  • C++ Compiler behavior regerding struct constructors
    ... I just notice some strange behaviors on the MS C++ compiler regarding struct ... struct MyStruct ... class MyClass ... MyClass::MyClass//The default ctor for the class does not init the struct ...
    (microsoft.public.dotnet.languages.vc)
  • Struggling with bsearch - on a struct!
    ... I have a struct with a string and a long member. ... item with nKey: 7. ... int compare(const void *val1, const void *val2); ... struct mystruct* sp1 = val1; ...
    (comp.lang.c)