CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2008
    Posts
    1

    object allocation question

    Hello Guru's -

    Am new to this forum, pls be kind.

    Can we create / instantiate objects inside static member functions of the same class?
    some thing like this?
    Why would people do like this??

    Class A
    {

    public:
    A(){}
    .
    .
    static void foo()
    {

    A* a = new A();

    }

    };

    thanks

  2. #2
    Join Date
    Jan 2008
    Location
    India
    Posts
    408

    Thumbs up Re: object allocation question

    Hello friend

    You can very well do that. Infact, you may have to do tat in the case of singleton class. Please have a look at this post

    http://www.codeguru.com/forum/showthread.php?t=344782

    Regards
    Bharani
    Rate the posts which you find useful

  3. #3
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    Re: object allocation question

    Ofcourse you can do that...
    What you can not do is this.

    Code:
    Class A
    {
    int a;
    public:
    A(){}
    .
    .
    static void foo()
    {
      a = 10; //You can not do this. Reason I let you find out..
    }
    
    };
    Dont forget to rate my post if you find it useful.

  4. #4
    Join Date
    Sep 2007
    Location
    Calcutta, India
    Posts
    95

    Re: object allocation question

    Hi,

    Welcom to the forum.

    You already got your answer. I Just wanted to bring you attention to this:

    Please always enclose any code or code snippet you are posting within the "code" tags ( tags with name "code", just like HTML tags, but ones which use the square brackets instead of the angled ones) which are vBulletin tags used for posting program code which encloses the code in a cute box and maintains the spacing and formatting/indentation of the code posted.

    You can find out all about them at : vB codes

    It's also a good idea to read the FAQ of this forum once before starting to use it regularly. It's quite informative.

    Best of Luck!
    I

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