STL & Copy constructor..help

N

Naren

Hello All,

I get an unexpected behaviour for this

class D{

/*something*/
};

class B
{
D *ptr;
/* something more */
};

class A
{
vector<B> Blist;

};
in some function of class A
for(i=0;i<n;i++)
{
B obj;
Blist.push_back(obj); //throws an exception
}

i have defined copy constructors for all the classes and used explicit
keyword also in the declaration

I think STL makes a copy using this copy constrcutor to store internally.Plz
throw some light

Thaanx in advance
Rgds,
Naren.
 
J

Jim Fischer

Naren said:
i have defined copy constructors for all the classes and used explicit
keyword also in the declaration

Comment 1) Are the assignment operator '=' methods for classes D and B
implemented correctly?

Comment 2) There is not enough information in your original post to
determine the actual problem. You really need to post the source code
for a short, complete, compilable program that excatly demonstrates the
problem you're describing. If we don't have some actual code to look at,
it's anyone's guess as to what the actual problem(s) might be.
 
K

Kevin Goodsell

Naren said:
Hello All,

I get an unexpected behaviour for this

class D{

/*something*/
};

class B
{
D *ptr;
/* something more */
};

class A
{
vector<B> Blist;

};
in some function of class A
for(i=0;i<n;i++)
{
B obj;
Blist.push_back(obj); //throws an exception

What exception?
}

i have defined copy constructors for all the classes and used explicit
keyword also in the declaration

I don't see any copy constructors.
I think STL makes a copy using this copy constrcutor to store internally.Plz
throw some light

The standard containers may copy using copy constructors or copy
assignment. Objects you store in them must support both operations with
the usual semantics.

Your problem is not apparent from what you posted. Please post a minimal
complete program that demonstrates the problem. You should *always* do
this when asking a question here.

-Kevin
 
J

John Harrison

Naren said:
Hello All,

I get an unexpected behaviour for this

class D{

/*something*/
};

class B
{
D *ptr;
/* something more */
};

class A
{
vector<B> Blist;

};
in some function of class A
for(i=0;i<n;i++)
{
B obj;
Blist.push_back(obj); //throws an exception
}

i have defined copy constructors for all the classes and used explicit
keyword also in the declaration

I think STL makes a copy using this copy constrcutor to store internally.Plz
throw some light

Yes the STL makes a copy using your copy constructor.

If you have defined your copy constructors correctly then this wouldn't be a
problem. Please post the code you are really using.
Thaanx in advance
Rgds,
Naren.

john
 

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

Forum statistics

Threads
474,142
Messages
2,570,817
Members
47,363
Latest member
eitamoro

Latest Threads

Top