[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Call Constructor for object in struct

David

9/29/2008 8:42:00 PM

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!!


10 Answers

Victor Bazarov

9/29/2008 8:47:00 PM

0

David wrote:
> How do I call the constructor when a class object (non-pointer) is in a
> structure?
>
> struct {
> ...
> CMyClass C;
> } MyStruct;
>
> MyStruct A, B;
>
> TIA!!

What do you mean by "call a constructor"? 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") };

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Juha Nieminen

9/29/2008 9:05:00 PM

0

David wrote:
> 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.

puzzlecracker

9/29/2008 9:17:00 PM

0

On Sep 29, 5:04 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> David wrote:
> > 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.

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

Any ideas?

Victor Bazarov

9/29/2008 9:26:00 PM

0

puzzlecracker wrote:
> [..]
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

puzzlecracker

9/29/2008 9:37:00 PM

0

On Sep 29, 5:25 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> puzzlecracker wrote:
> > [..]
> > 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
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask

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?

Victor Bazarov

9/29/2008 9:43:00 PM

0

puzzlecracker wrote:
> On Sep 29, 5:25 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>> puzzlecracker wrote:
>>> [..]
>>> 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
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask
>
> 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?

Erm... There is *no* "pointer version copy ctor". Copy constructors
*always* take a reference argument. And, no, if you define some other
constructor, the compiler will still provide a copy constructor in the
former interface, if it can (depends whether the members are copyable).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

James Kanze

9/30/2008 9:05:00 AM

0

On Sep 29, 10:47 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> David wrote:
> > How do I call the constructor when a class object
> > (non-pointer) is in a structure?

> > struct {
> > ...
> > CMyClass C;
> > } MyStruct;

> > MyStruct A, B;

> 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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze

9/30/2008 9:07:00 AM

0

On Sep 29, 11:25 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:

[...]
> 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.

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...)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

David

9/30/2008 8:51:00 PM

0

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();

??

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:gbres6$r8v$3@news.datemas.de...
> David wrote:
>> How do I call the constructor when a class object (non-pointer) is in a
>> structure?
>>
>> struct {
>> ...
>> CMyClass C;
>> } MyStruct;
>>
>> MyStruct A, B;
>>
>> TIA!!
>
> What do you mean by "call a constructor"? 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") };
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask
>


James Kanze

10/1/2008 9:23:00 AM

0

On Sep 30, 10:51 pm, "David" <no...@nowhere.com> wrote:
> 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++.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34