C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

define a copy constructor in a class having data member as a

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Thu Nov 09, 2006 5:12 pm    Post subject: define a copy constructor in a class having data member as a Reply with quote



Hi,
can anyone help me by writing a sample code of defining a copy
constructor in a class having data member as an object of another
class.
for eg:
class A{
int x;
public: A(){ x=6;}


};


class B{
A a1;
public:B(B &b1)
{ ???} //how i can assign the data members??


};


Any idea ...Thanks


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Guest






PostPosted: Thu Nov 09, 2006 7:37 pm    Post subject: Re: define a copy constructor in a class having data member Reply with quote



The contained object itself should have copy constructor and assignment
operator defined unless the object is a POD (plain old data) type.

On Nov 9, 4:12 pm, dalu.g...@gmail.com wrote:
Quote:
Hi,
can anyone help me by writing a sample code of defining a copy
constructor in a class having data member as an object of another
class.
for eg:
class A{
int x;
public: A(){ x=6;}

};class B{
A a1;
public:B(B &b1)
{ ???} //how i can assign the data members??

};Any idea ...Thanks

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Dildo Baggins
Guest





PostPosted: Thu Nov 09, 2006 7:38 pm    Post subject: Re: define a copy constructor in a class having data member Reply with quote



dalu.gelu (AT) gmail (DOT) com wrote:

Quote:
Hi,
can anyone help me by writing a sample code of defining a copy
constructor in a class having data member as an object of another
class.
for eg:
class A{
int x;
public: A(){ x=6;}


};


class B{
A a1;
public:B(B &b1)
{ ???} //how i can assign the data members??


};


class B {
A a1 ;
public:
// since you're not going to modify b1, it's better to
// declare the param as const
//
B( const B& b1 ):
a1( b1.a1 )
{ }

};

in this particular case, it's not necessary to define a copy-ctor for
class A,
since the default one will be ok.

--
SF

"Yo! Quota! Do that quoting thing, baby [...]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
AlbertSSj
Guest





PostPosted: Thu Nov 09, 2006 7:38 pm    Post subject: Re: define a copy constructor in a class having data member Reply with quote

Quote:
class A{
int x;
public: A(){ x=6;}
};


Quote:
class B{
A a1;
public:B(B &b1)
{ ???} //how i can assign the data members??
};


B(const B& b1) // Is better to use constness whenever possible
: a1(b1.a1) // Call copy constructor of class A
{}

Since x is in the private part of A you cannot assign him
directly from B, unless you declare B as friend of A.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ulrich Eckhardt
Guest





PostPosted: Thu Nov 09, 2006 7:40 pm    Post subject: Re: define a copy constructor in a class having data member Reply with quote

dalu.gelu (AT) gmail (DOT) com wrote:
Quote:
can anyone help me by writing a sample code of defining a copy
constructor in a class having data member as an object of another
class.
for eg:
class A{
int x;
public: A(){ x=6;}
};

class B{
A a1;
public:B(B &b1)
{ ???} //how i can assign the data members??
};

I think that you want something like

a1 = b1.a1;

However, please look up the chapter in your book where constructors, copy
constructors and assignment operators are explained. Further, in a
constructor you should prefer initialisation, in the case of class A that
would be this:

A::A():
x(6) // initialise x with 6
{} // empty

for class B that would be

B::B( B const& b1):
a1( b1.a1) // init with content of other B
{} // empty


One more thing: there alt.comp.lang.learn.c-c++, which is particularly
geared at beginners and (since it is not moderated) you will get answers
much quicker typically, which is very convenient in a time when even small
problems can be complete showstoppers.

HTH

Uli


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Thu Nov 09, 2006 7:41 pm    Post subject: Re: define a copy constructor in a class having data member Reply with quote

In article <1163064981.465677.147390 (AT) m73g2000cwd (DOT) googlegroups.com>,
dalu.gelu (AT) gmail (DOT) com writes
Quote:
Hi,
can anyone help me by writing a sample code of defining a copy
constructor in a class having data member as an object of another
class.
for eg:
class A{
int x;
public: A(){ x=6;}


};


class B{
A a1;
public:B(B &b1)
{ ???} //how i can assign the data members??


};

Use a constuctor initialiser list. In the above case:


B::B(B const & b1): a1(b1.a1){ /* whatever */}

Assignment to data members in constructors is always suspect (i.e. you
should check that it is not better done in the constructor init list).

Also note that copy constructors take the argument by reference to const
unless there are special reasons not to (else you cannot copy const
qualified instances).


--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.