C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to make an object of a class instantiable only on the h
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
vijay.y.kumar@gmail.com
Guest





PostPosted: Wed Dec 22, 2004 8:52 am    Post subject: how to make an object of a class instantiable only on the h Reply with quote



Hi

I want to have my class instantiated on the heap and not on the stack.
Please correct if this is not possible in C++.

class XYZ
{
....
};

int main ()
{
XYZ a ; //Should cause a compiler error.

XYZ *p = new XYZ() ; //Should not cause problems.
}

How can i achieve this.

Thanks


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Sharad Kala
Guest





PostPosted: Thu Dec 23, 2004 11:51 am    Post subject: Re: how to make an object of a class instantiable only on t Reply with quote




<vijay.y.kumar (AT) gmail (DOT) com> wrote in message
Quote:
Hi

I want to have my class instantiated on the heap and not on the stack.
Please correct if this is not possible in C++.

class XYZ
{
...
};

int main ()
{
XYZ a ; //Should cause a compiler error.

XYZ *p = new XYZ() ; //Should not cause problems.
}

How can i achieve this.

Consider this -

class XYZ{
public:
XYZ(){}
void destroy() const { delete this;}
private:
~XYZ(){}
};

int main()
{
XYZ a1; // error
XYZ* a2 = new XYZ;
a2->destroy();
}

Sharad



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
gareth_stockwell@hotmail.
Guest





PostPosted: Thu Dec 23, 2004 11:51 am    Post subject: Re: how to make an object of a class instantiable only on th Reply with quote




Quote:
I want to have my class instantiated on the heap and not on the
stack.
Please correct if this is not possible in C++.

Vijay,

Simply make the constructor private, and add a static member function
which allocates an object on the stack:

class XYZ {

XYZ() { }

public:
static XYZ* create() { return new XYZ; }
};

int main() {

XYZ a; // error
XYZ* pxyx = XYZ::create(); // OK
}

By the way, the C++ FAQ lite...
http://www.parashift.com/c++-faq-lite

.... is a good place to look for answers of this kind. In fact it has
just the one you want:
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.20
Gareth


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ivan Vecerina
Guest





PostPosted: Thu Dec 23, 2004 11:53 am    Post subject: Re: how to make an object of a class instantiable only on t Reply with quote

<vijay.y.kumar (AT) gmail (DOT) com> wrote

Quote:
I want to have my class instantiated on the heap and not on the stack.
Please correct if this is not possible in C++.

class XYZ
{
...
};

int main ()
{
XYZ a ; //Should cause a compiler error.

XYZ *p = new XYZ() ; //Should not cause problems.
}

How can i achieve this.
Make a private constructor, and provide a factory function:


in the class:
static XYZ* create() { return new XYZ(); }

usage:
XYZ *p = XYZ::create();


hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Falk Tannhäuser
Guest





PostPosted: Thu Dec 23, 2004 11:54 am    Post subject: Re: how to make an object of a class instantiable only on t Reply with quote

[email]vijay.y.kumar (AT) gmail (DOT) com[/email] wrote:
Quote:
I want to have my class instantiated on the heap and not on the stack.
Please correct if this is not possible in C++.

Make all constructors private (or protected if you want to derive),
and supply (a) public static member function(s) 'Create()' to
perform the 'new'. To call the copy constructor, a non-static
member function Clone() will do, too.

Quote:
class XYZ
{
...

protected:
XYZ();
XYZ(char const* name, int number); // or whatever
XYZ(XYZ const&);
public:
static XYZ* Create() { return new XYZ; }
static XYZ* Create(char const* name, int number) { return new XYZ(name, number); }
XYZ* Clone() const { return new XYZ(*this); } // could be virtual if needed
Quote:
}; // class XYZ

int main ()
{
XYZ a ; //Should cause a compiler error.

XYZ *p = new XYZ() ; //Should not cause problems.
XYZ* p = XYZ::Create();

XYZ* p2 = p->Clone();
Quote:
}

Falk

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Heinz Ozwirk
Guest





PostPosted: Thu Dec 23, 2004 11:55 am    Post subject: Re: how to make an object of a class instantiable only on t Reply with quote

<vijay.y.kumar (AT) gmail (DOT) com> schrieb im Newsbeitrag news:1103689018.111147.43270 (AT) f14g2000cwb (DOT) googlegroups.com...
Quote:
Hi

I want to have my class instantiated on the heap and not on the stack.
Please correct if this is not possible in C++.

class XYZ
{
...
};

int main ()
{
XYZ a ; //Should cause a compiler error.

XYZ *p = new XYZ() ; //Should not cause problems.
}

How can i achieve this.

Declare only private constructors and add a static function to create a new instance:

class XYZ
{
public:
static XYZ* Create();

private:
XYZ();

...
};

....

XYZ* XYZ::Create()
{
return new XYZ;
}

However, you cannot use new to create an instance of such classes, you have to call the create function instead:

XYZ* p = XYZ::Create();

HTH
Heinz

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bob Hairgrove
Guest





PostPosted: Thu Dec 23, 2004 3:13 pm    Post subject: Re: how to make an object of a class instantiable only on t Reply with quote

On 22 Dec 2004 03:52:18 -0500, [email]vijay.y.kumar (AT) gmail (DOT) com[/email] wrote:

Quote:
Hi

I want to have my class instantiated on the heap and not on the stack.
Please correct if this is not possible in C++.

class XYZ
{
...
};

int main ()
{
XYZ a ; //Should cause a compiler error.

XYZ *p = new XYZ() ; //Should not cause problems.
}

How can i achieve this.


Item 27 of Scott Meyers "More Effective C++" shows how to do it. If
you don't have this book, and its companion "Effective C++", I
recommend that you read both of them.

The answer for simple cases is to make the destructor and/or the
constructors private (or protected for base classes), providing
explicit factory functions for creation and a public member function
for deletion which calls "delete this;".

However, it can get rather involved when you have to determine whether
an object is actually on the heap or not (for example, if you have
public constructors and a protected destructor). It's much simpler to
use the abstract factory method for polymorphic classes and return a
pointer or reference to the base class.

--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
MANOJ M RAMACHANDRAN
Guest





PostPosted: Thu Dec 23, 2004 3:23 pm    Post subject: Re: how to make an object of a class instantiable only on t Reply with quote

Make the class XYZ's constructor private and instanstiate the class using
another public member (e.g. Instance() ).

class XYZ
{
private:
XYZ();
public
XYZ* Instance()
{
return new XYZ();
}
};

Manoj.

<vijay.y.kumar (AT) gmail (DOT) com> wrote

Quote:
Hi

I want to have my class instantiated on the heap and not on the stack.
Please correct if this is not possible in C++.

class XYZ
{
...
};

int main ()
{
XYZ a ; //Should cause a compiler error.

XYZ *p = new XYZ() ; //Should not cause problems.
}

How can i achieve this.
[excessive quoting deleted --mod]


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Antoun Kanawati
Guest





PostPosted: Thu Dec 23, 2004 3:30 pm    Post subject: Re: how to make an object of a class instantiable only on t Reply with quote

[email]vijay.y.kumar (AT) gmail (DOT) com[/email] wrote:
Quote:
Hi

I want to have my class instantiated on the heap and not on the stack.
Please correct if this is not possible in C++.

class XYZ
{
...
};

int main ()
{
XYZ a ; //Should cause a compiler error.

XYZ *p = new XYZ() ; //Should not cause problems.
}

The usual manner is to hide the constructor, and have a factory
method.

If the constructor is visible, there is little that you can do
to inhibit its use.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Chris Uzdavinis
Guest





PostPosted: Thu Dec 23, 2004 5:07 pm    Post subject: Re: how to make an object of a class instantiable only on th Reply with quote

Quote:
I want to have my class instantiated on the heap and not on the
stack.
Please correct if this is not possible in C++.

In a limited way it can be accomplished. Make all constructors
private, and write a static member function called allocate(), which
returns a new instance of your class. If your constructor(s) take
arguments, write corresponding allocate functions taking the same
parameters, passing them to the constructor:

class X
{
public:
static X * allocate(int i) { return new X(i); }
static X * allocate() { return newX; }
private:
X();
X(int);
X(X const &); // important to restrict cpy ctor.
};

This makes it impossible to inherit from the class, but I don't see any
way around that, other than making the constructors "protected", but
then you lose your enforcement of the object being allocated from the
heap:

class Y : public X
{
};

Now a Y can be created on the stack, and it creates X on the stack too.
You can't enforce that derived types uphold your rules, so I prefer to
prevent that possibility by making the constructor private.

--
Chris


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ahti Legonkov
Guest





PostPosted: Thu Dec 23, 2004 9:02 pm    Post subject: Re: how to make an object of a class instantiable only on t Reply with quote

[email]vijay.y.kumar (AT) gmail (DOT) com[/email] wrote:

Quote:
I want to have my class instantiated on the heap and not on the stack.
Please correct if this is not possible in C++.

It is possible.

....
Quote:

int main ()
{
XYZ a ; //Should cause a compiler error.

XYZ *p = new XYZ() ; //Should not cause problems.
}

How can i achieve this.

Make destructor private. And then you probably want to use some
smart pointer to clean up this object. In this case you may want to
make that smart pointer friend of your class. If not then just
provide a member function that calls "delete this;".


--
Ahti Legonkov
leg zero at hot dot ee


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Pavel Vozenilek
Guest





PostPosted: Thu Dec 23, 2004 9:08 pm    Post subject: Re: how to make an object of a class instantiable only on t Reply with quote


Quote:
I want to have my class instantiated on the heap and not on the stack.
Please correct if this is not possible in C++.

Use object factory:


class XYZ
{
protected:
XYZ() {...}
~XYZ() {...}
public:
XYZ* createXYZ(parameters) { return new XYZ(parameters); }
....
};

To avoid problems with resource handling take look
on std::auto_ptr or boost::smart_ptr.

/Pavel



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jay Nabonne
Guest





PostPosted: Thu Dec 23, 2004 9:17 pm    Post subject: Re: how to make an object of a class instantiable only on t Reply with quote

On Wed, 22 Dec 2004 03:52:18 -0500, vijay.y.kumar wrote:

Quote:
Hi

I want to have my class instantiated on the heap and not on the stack.
Please correct if this is not possible in C++.

class XYZ
{
...
};

int main ()
{
XYZ a ; //Should cause a compiler error.

XYZ *p = new XYZ() ; //Should not cause problems.
}

How can i achieve this.


AFAIK, you can't do it exactly like that. But you can do:

class XYZ
{
private:
// Private constructor.
XYZ(){}

public:
XYZ* New() { return new XYZ; }
};

int main()
{
XYZ* p = XYZ::New();
}

- Jay

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
wkaras@yahoo.com
Guest





PostPosted: Thu Dec 23, 2004 9:19 pm    Post subject: Re: how to make an object of a class instantiable only on th Reply with quote

[email]vijay.y.kumar (AT) gmail (DOT) com[/email] wrote:
Quote:
Hi

I want to have my class instantiated on the heap and not on the
stack.
Please correct if this is not possible in C++.

class XYZ
{
...
};

int main ()
{
XYZ a ; //Should cause a compiler error.

XYZ *p = new XYZ() ; //Should not cause problems.
}

....


Could you live with calling an allocator function instead of using new?

class XYZ
{
private:
XYZ(void) { }

public:
static XYZ * allocXYZ(void) { return(new XYZ); }
};

If you can't (maybe you're passing the class to a template that uses
new),
another alternative is:


#include <stdlib.h>

class XYZ2
{
private:
// Initialized to false.
static bool new_just_called;

public:

void * operator new (size_t sz)
{
register void *p = ::operator new(sz);
new_just_called = true;
return(p);
}

XYZ2(void)
{
if (!new_just_called)
exit(1);
new_just_called = false;

// ...
}
};

But clearly this could not be used in a multi-threaded process where
instances of XYZ2 were created in more than one thread. In that case,
you would need to have a copy of new_just_called per each thread, but
there's no portable way of doing that that I know of.

Note that when this function:

XYZ2 *foo(void) { return(new XYZ2); }

is compiled by GCC with optimization enabled, the run-time overhead
of the "heap-only" logic is reduced to setting the new_just_called
flag to false.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
L.Suresh
Guest





PostPosted: Thu Dec 23, 2004 9:22 pm    Post subject: Re: how to make an object of a class instantiable only on th Reply with quote

Make the destructor non-public and delete the object allocated on the
heap using a member function or some other method.

You may want to look at Scott Meyers Item 27 (More Effective C++),
"Requiring or prohibiting Heap-Based Objects"

--lsu


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.