Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Newing objects in constructor

Reply
Thread Tools

Newing objects in constructor

 
 
tech
Guest
Posts: n/a
 
      06-05-2008
Hi, Whenever i have a class that contains references to other classes
i keep
end up doing the below. However it doesn't seem so safe or elegant.
How do the
pros initialise subobjects, any ideas to improve the below would be
welcome.

/.h file
class A
{
public:
A();
~A();
private:
SomeMethod();

obj1* pobj1;
obj2* pobj2;
obj3* pobj3;
obj4* pobj4;
};

/.cpp file

A::A()
{
pobj1 = new obj1;
pobj2 = new obj2;
pobj3 = new obj3;
pobj4 = new obj4;

}

A::SomeMethod()
{

pobj1->doSomething();
etc
}

A::~A()
{
delete pobj1;
delete pobj2;
delete pobj3;
delete pobj4;
}
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      06-05-2008
tech wrote:

> Hi, Whenever i have a class that contains references to other classes
> i keep
> end up doing the below. However it doesn't seem so safe or elegant.
> How do the
> pros initialise subobjects, any ideas to improve the below would be
> welcome.
>
> /.h file
> class A
> {
> public:
> A();
> ~A();
> private:
> SomeMethod();
>
> obj1* pobj1;
> obj2* pobj2;
> obj3* pobj3;
> obj4* pobj4;
> };
>
> /.cpp file
>
> A::A()
> {
> pobj1 = new obj1;
> pobj2 = new obj2;
> pobj3 = new obj3;
> pobj4 = new obj4;


If the last new throws, you leak the memory for the first three objects. If
you _really_ need pointers at all, consider using std::auto_ptr during
initialization:

A::A() {
std::auto_ptr< obj1 > dummy1 ( new obj1 );
std::auto_ptr< obj2 > dummy2 ( new obj2 );
std::auto_ptr< obj3 > dummy3 ( new obj3 );
std::auto_ptr< obj4 > dummy4 ( new obj4 );
pobj1 = dummy1;
pobj2 = dummy2;
pobj3 = dummy3;
pobj4 = dummy4;
}

>
> }
>
> A::SomeMethod()
> {
>
> pobj1->doSomething();
> etc
> }
>
> A::~A()
> {
> delete pobj1;
> delete pobj2;
> delete pobj3;
> delete pobj4;
> }


If you go with those pointers, you either need to make the assignment
operator and copy constructor private or implement them in some way that
does the RightThing(tm), whatever that would be in your case. The ones
generated by the compiler will _not_ do the right thing.


More importantly: why do you want pointers in the first place? You could
just do

class A {
type1 obj1;
type2 obj2;
...
};

Nothing in your post shows a genuine need for pointer members.


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
Frank Birbacher
Guest
Posts: n/a
 
      06-05-2008
Hi!

Kai-Uwe Bux schrieb:
> A::A() {
> std::auto_ptr< obj1 > dummy1 ( new obj1 );
> std::auto_ptr< obj2 > dummy2 ( new obj2 );
> std::auto_ptr< obj3 > dummy3 ( new obj3 );
> std::auto_ptr< obj4 > dummy4 ( new obj4 );
> pobj1 = dummy1;


You need to "release()" the object from the auto_ptr:
pobj1 = dummy1.release();

But it would be easier to just use auto_ptrs as members (if you _really_
need pointers after all):

class A {
//use const until you implement operator =
const std::auto_ptr<obj1> pobj1;
A();
};

A::A()
: pobj1(new obj1)
{}

Frank
 
Reply With Quote
 
bjeremy
Guest
Posts: n/a
 
      06-05-2008

>
> If the last new throws, you leak the memory for the first three objects. If
> you _really_ need pointers at all, consider using std::auto_ptr during
> initialization:
>
> A::A() {
> std::auto_ptr< obj1 > dummy1 ( new obj1 );
> std::auto_ptr< obj2 > dummy2 ( new obj2 );
> std::auto_ptr< obj3 > dummy3 ( new obj3 );
> std::auto_ptr< obj4 > dummy4 ( new obj4 );
> pobj1 = dummy1;
> pobj2 = dummy2;
> pobj3 = dummy3;
> pobj4 = dummy4;
> }



Actually, I could be seeing something correctly, but won't all your
pointers be destroyed as soon as you leave the scope of the
constructor? Don't you either have to explicitly release the auto_ptr
(i.e. dummy1.release()) after the assignment... of if you can,
declare your pointers in your class as auto_ptrs i.e.:

/.h file
class A
{
public:
A();
~A();
private:
SomeMethod();

std::auto_ptr< obj1 > myobj1;
std::auto_ptr< obj2 > myobj2;
std::auto_ptr< obj3 > myobj3;
std::auto_ptr< obj4 > myobj4;

};

A::A()
: myobj1(new obj1),
myobj2(new obj2),
myobj3(new obj3),
myobj4(new obj4)
{
}

Of course, they do not have to be auto_ptrs I guess... but using some
type of smart pointer in the declaration may make your life easier
than just using raw pointers.
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      06-05-2008
bjeremy wrote:

>
>>
>> If the last new throws, you leak the memory for the first three objects.
>> If you _really_ need pointers at all, consider using std::auto_ptr during
>> initialization:
>>
>> A::A() {
>> std::auto_ptr< obj1 > dummy1 ( new obj1 );
>> std::auto_ptr< obj2 > dummy2 ( new obj2 );
>> std::auto_ptr< obj3 > dummy3 ( new obj3 );
>> std::auto_ptr< obj4 > dummy4 ( new obj4 );
>> pobj1 = dummy1;
>> pobj2 = dummy2;
>> pobj3 = dummy3;
>> pobj4 = dummy4;
>> }

>
>
> Actually, I could be seeing something correctly, but won't all your
> pointers be destroyed as soon as you leave the scope of the
> constructor?


Oops. That should be

pobj1 = dummy1.release();
...

> Don't you either have to explicitly release the auto_ptr
> (i.e. dummy1.release()) after the assignment... of if you can,
> declare your pointers in your class as auto_ptrs i.e.:
>
> /.h file
> class A
> {
> public:
> A();
> ~A();
> private:
> SomeMethod();
>
> std::auto_ptr< obj1 > myobj1;
> std::auto_ptr< obj2 > myobj2;
> std::auto_ptr< obj3 > myobj3;
> std::auto_ptr< obj4 > myobj4;
>
> };
>
> A::A()
> : myobj1(new obj1),
> myobj2(new obj2),
> myobj3(new obj3),
> myobj4(new obj4)
> {
> }
>
> Of course, they do not have to be auto_ptrs I guess... but using some
> type of smart pointer in the declaration may make your life easier
> than just using raw pointers.


Yes.


Thanks

Kai-Uwe Bux
 
Reply With Quote
 
tech
Guest
Posts: n/a
 
      06-09-2008
Thanks for all the replies, one further question

if i don't usse pointers but object members instead i need to include
the header files rather than forward declare. Isn't this an argument
against having member objects?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Can you delete a pointer without newing it up? (modified version) asimorio C++ 4 05-31-2006 05:18 PM
Can you delete a pointer without newing it up? asimorio C++ 11 05-31-2006 03:15 PM
why it's not possible calling constructor from constructor? Giulio C++ 9 06-25-2003 03:56 PM



Advertisments