Call Constructor for object in struct

D

David

Hello,

How do I call the constructor when a class object (non-pointer) is in a
structure?

struct {
...
CMyClass C;
} MyStruct;

MyStruct A, B;

TIA!!
 
J

Juha Nieminen

David said:
Hello,

How do I call the constructor when a class object (non-pointer) is in a
structure?

struct {
...
CMyClass C;
} MyStruct;

MyStruct A, B;

You write a constructor in your struct which calls the constructor of
the member class.
 
P

puzzlecracker

  You write a constructor in your struct which calls the constructor of
the member class.

I wonder why C++ is in favor of CMyClass(char const*); copy
constructor then CMyClass(char const &);

Any ideas?
 
P

puzzlecracker

puzzlecracker said:
[..]
I wonder why  C++ is in favor of  CMyClass(char const*); copy
constructor  then      CMyClass(char const &);
Any ideas?

Please rephrase your statement about C++ and favors.  A copy constructor
for the 'CMyClass' class would be either

     CMyClass(CMyClass const &);

or

     CMyClass(CMyClass &);

Anything else is *not* a copy constructor.

V

No need to rephrase, you answered it. I'm assuming, by providing one
of (pointer or reference version) copy ctors, the other one will not
be generated by compiler by default. Is my assumption correct, aka
reflected in the standard?
 
J

James Kanze

What do you mean by "call a constructor"?

According to the standard, you can't "call a constructor:)".
At any rate, what he probably wants is to initialize the
elements.
What constructors does 'CMyClass' have? Perhaps this is going
to help:
class CMyClass {
public:
CMyClass(int);
CMyClass(char const*);
};

MyStruct A = { ..., CMyClass(42) }, B = { ..., CMyClass("David") };

Note that in this case, you don't even need the CMyClass. If
the constructors take a single argument, and aren't declared
explicit, explicit conversion will take place.
 
J

James Kanze

[...]
Please rephrase your statement about C++ and favors. A copy constructor
for the 'CMyClass' class would be either
CMyClass(CMyClass const &);

CMyClass(CMyClass &);
Anything else is *not* a copy constructor.

MyClass( MyClass volatile& ) ;
MyClass( MyClass const volatile& ) ;

:).

(Of course, I've never found a use for volatile except on very
basic types, and it doesn't work with most modern compilers
anyway. But formally...)
 
D

David

Yeah, basically that... Would this work:

struct {
int a,b,c,d,e,f,g,h,i,j;
CMyClass C;
} MyStruct;

MyStruct A={CMyClass(whatever))};

or would I have to always deal with a through j?

How about:

struct {
int a,b,c,d,e,f,g,h,i,j;
CMyClass C;

void Init(void) {
a=b=c=d=e=f=g=h=i=j=0;
C(whatever)
}
} MyStruct;

MyStruct A;

A.Init();

??
 
J

James Kanze

Yeah, basically that... Would this work:
struct {
int a,b,c,d,e,f,g,h,i,j;
CMyClass C;
} MyStruct;
MyStruct A={CMyClass(whatever))};
or would I have to always deal with a through j?

Agglomerate initialization is always in order within a single
object. What you could do is:

struct MyStruct {
struct { int a, b, c, d, e, f, g, h, i, j ; } intData ;
MyClass classData ;
} ;

MyStruct a = { {}, MyClass( whatever ) } ;

(I'd definitely drop the C in front of the class name. It's
typically an indication that the class is part of MFC.)
How about:
struct {
int a,b,c,d,e,f,g,h,i,j;
CMyClass C;
void Init(void) {
a=b=c=d=e=f=g=h=i=j=0;
C(whatever)
}
} MyStruct;

First, that's

struct MyStruct { ... } ;

The name of the type comes immediately after the class key (one
of the keywords class, struct or union). What you've written
defines a variable MyStruct with an anonymous class type.

And you're generally better off using an initializer list in the
constructor:

MyClass::MyClass()
: a( 0 )
, b( 0 )
// ...
, C( whatever )
{
}

I'd suggest that you acquire a good C++ introductory text, and
read it. You really do need to know about constructors, etc.,
if you're going to use C++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top