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
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
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..
}
};
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