Click to See Complete Forum and Search --> : object allocation question


tsanju
April 2nd, 2008, 09:10 PM
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

ajbharani
April 3rd, 2008, 12:06 AM
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

code_carnage
April 3rd, 2008, 01:25 AM
Ofcourse you can do that...
What you can not do is this.


Class A
{
int a;
public:
A(){}
.
.
static void foo()
{
a = 10; //You can not do this. Reason I let you find out..
}

};

indrajit_p1
April 3rd, 2008, 08:51 AM
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 (http://www.codeguru.com/forum/misc.php?do=bbcode)

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