Connecting Tech Pros Worldwide Forums | Help | Site Map

STL & Copy constructor..help

Naren
Guest
 
Posts: n/a
#1: Jul 19 '05
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.






Jim Fischer
Guest
 
Posts: n/a
#2: Jul 19 '05

re: STL & Copy constructor..help


Naren wrote:[color=blue]
> i have defined copy constructors for all the classes and used explicit
> keyword also in the declaration[/color]

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.

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com


John Harrison
Guest
 
Posts: n/a
#3: Jul 19 '05

re: STL & Copy constructor..help



"Naren" <narendranath.ts@in.bosch.com> wrote in message
news:bimhvm$cvp$1@ns2.fe.internet.bosch.com...[color=blue]
> 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[/color]
internally.Plz[color=blue]
> throw some light[/color]

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.
[color=blue]
>
> Thaanx in advance
> Rgds,
> Naren.
>[/color]

john


Kevin Goodsell
Guest
 
Posts: n/a
#4: Jul 19 '05

re: STL & Copy constructor..help


Naren wrote:
[color=blue]
> 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[/color]

What exception?
[color=blue]
> }
>
> i have defined copy constructors for all the classes and used explicit
> keyword also in the declaration[/color]

I don't see any copy constructors.
[color=blue]
>
> I think STL makes a copy using this copy constrcutor to store internally.Plz
> throw some light[/color]

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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Closed Thread