|
-
April 2nd, 2008, 09:10 PM
#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
-
April 3rd, 2008, 12:06 AM
#2
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
-
April 3rd, 2008, 01:25 AM
#3
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.
-
April 3rd, 2008, 08:51 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|