CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    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

  2. #2
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    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

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: which design is better to wrap another class instance

    This has been answered elsewhere.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    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? :-)

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured