Hi! A petty doubt before geeks!!

Discussion in 'C++' started by nikki, Jan 17, 2007.

  1. nikki

    nikki Guest

    Hi all!

    I have alittle question.
    What is the code like to make sure that if we create an object of a
    class dynamically , it is allocated memory but not when a static object
    of the same class????
     
    nikki, Jan 17, 2007
    #1
    1. Advertising

  2. nikki

    Noah Roberts Guest

    Re: Hi! A petty doubt before geeks!!

    nikki wrote:
    > Hi all!
    >
    > I have alittle question.
    > What is the code like to make sure that if we create an object of a
    > class dynamically , it is allocated memory but not when a static object
    > of the same class????


    Privatize the constructor and create a factory function.
     
    Noah Roberts, Jan 17, 2007
    #2
    1. Advertising

  3. nikki wrote:
    > What is the code like to make sure that if we create an object of a
    > class dynamically , it is allocated memory but not when a static
    > object of the same class????


    Not sure what you mean here, but if you want to require the objects
    of your class always created dynamically, you should provide a factory
    method (which would return a pointer) and make constructors private.

    I hope it's OK that I responded. I am not a geek, I just hang out
    here.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 17, 2007
    #3
  4. nikki

    Guest

    Re: Hi! A petty doubt before geeks!!

    Victor Bazarov wrote:
    > nikki wrote:
    > > What is the code like to make sure that if we create an object of a
    > > class dynamically , it is allocated memory but not when a static
    > > object of the same class????

    >
    > Not sure what you mean here, but if you want to require the objects
    > of your class always created dynamically, you should provide a factory
    > method (which would return a pointer) and make constructors private.
    >
    > I hope it's OK that I responded. I am not a geek, I just hang out
    > here.


    No, you're not a geek! You're even worse! You're the C++ language
    police! Aaaaah!
     
    , Jan 18, 2007
    #4
  5. * nikki:
    > Hi all!
    >
    > I have alittle question.
    > What is the code like to make sure that if we create an object of a
    > class dynamically , it is allocated memory but not when a static object
    > of the same class????


    If you mean, "how do I ensure that objects of my class are dynamically
    allocated?":

    Make the destructor 'private' or 'protected', and provide an accessible
    function that calls 'delete'. This can be an ordinary member function
    (simple) or e.g. a friend template function (general).

    This way you don't have to provide one factory function per constructor,
    which is less work and better for maintenance (adding, removing or
    changing constructors requires no parallell maintenance of factory
    functions, and deriving from the class can be easier).

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jan 18, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    659
  2. rick

    Books about computer geeks?

    rick, Oct 17, 2006, in forum: C Programming
    Replies:
    20
    Views:
    825
    Keith Thompson
    Oct 17, 2006
  3. cplusplus

    GEEKS

    cplusplus, Oct 29, 2007, in forum: C Programming
    Replies:
    0
    Views:
    420
    cplusplus
    Oct 29, 2007
  4. James Edward Gray II

    My Fellow Geeks

    James Edward Gray II, Oct 13, 2006, in forum: Ruby
    Replies:
    0
    Views:
    123
    James Edward Gray II
    Oct 13, 2006
  5. Ramon F Herrera

    The Revenge of the Geeks

    Ramon F Herrera, Jan 22, 2013, in forum: Java
    Replies:
    95
    Views:
    1,171
    Gene Wirchenko
    Feb 5, 2013
Loading...

Share This Page