|
-
April 12th, 2008, 03:54 AM
#1
which design is better to wrap another class instance
Hello everyone,
Some people doubt that the design below is inferior compared to design which makes f, either Foo* or Foo&. The reason is below design will waste memory space and degrade performance by creating a new instance.
I think it depends. If we really needs to wrap a new instance, I think the following design is fine. If we needs to refers to an existing instance, this design is inferior because it will waste memory space and degrade performance by creating a new instance and destroy the original one.
Code:
class Foo;
class Goo
{
Foo f; // change to Foo* pf or Foo& rf is always better?
}
thanks in advance,
George
-
April 12th, 2008, 04:05 AM
#2
Re: which design is better to wrap another class instance
I think it actually depends on what is Foo and what is Goo and what you are trying to achieve.
In your example the Goo object actually owns the Foo object. But if actually Foo doesn't belong to Goo and Goo only needs accessing Foo then you can use a pointer or a reference.
Pointer can also be used to get an extra level of indirection (pImpl Idiom) and to reduce compilation time.
If there is no love sun won't shine
-
April 12th, 2008, 04:17 AM
#3
Re: which design is better to wrap another class instance
This has been answered elsewhere.
-
April 12th, 2008, 04:19 AM
#4
Re: which design is better to wrap another class instance
Thanks miteshpandey,
I think your point is if owns the Goo, using class type other than pointer/reference type is better. Right?
But how do you define or judge whether "owns" or not? In my perpective, I think it means managing the lifecycle (create and destroy), I am not sure whether you have different ideas about what means own? :-)
 Originally Posted by miteshpandey
I think it actually depends on what is Foo and what is Goo and what you are trying to achieve.
In your example the Goo object actually owns the Foo object. But if actually Foo doesn't belong to Goo and Goo only needs accessing Foo then you can use a pointer or a reference.
Pointer can also be used to get an extra level of indirection (pImpl Idiom) and to reduce compilation time.
have a good weekend,
George
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|