Discussion:
Class Instantiation
(too old to reply)
John Scott
2008-05-15 13:54:09 UTC
Permalink
I can create an instance of a class, say, Counter for example, as follows.

Counter *C1 = new Counter.

and everything is fine.



However, I get a compiler error when I call a function and try to make more
than one instance. For example



SomeFunction( )

{

static int z = 0;

Counter *C(z) = new Counter;

}



Could anyone please explain what I am doing wrong or if there is another way
of achieving the same result.

Many Thanks.
Bruce Salzman
2008-05-15 14:04:08 UTC
Permalink
Post by John Scott
I can create an instance of a class, say, Counter for example, as follows.
Counter *C1 = new Counter.
and everything is fine.
However, I get a compiler error when I call a function and try to make more
than one instance. For example
SomeFunction( )
{
static int z = 0;
Counter *C(z) = new Counter;
}
Could anyone please explain what I am doing wrong or if there is another way
of achieving the same result.
If you are trying to pass z to Counter's constructor, the proper syntax is:
Counter *C = new Counter(z);
--
Bruce
Vladimir Grigoriev
2008-05-15 14:07:42 UTC
Permalink
Post by John Scott
I can create an instance of a class, say, Counter for example, as follows.
Counter *C1 = new Counter.
and everything is fine.
However, I get a compiler error when I call a function and try to make
more than one instance. For example
SomeFunction( )
{
static int z = 0;
Counter *C(z) = new Counter;
}
Could anyone please explain what I am doing wrong or if there is another
way of achieving the same result.
Many Thanks.
The statement
Counter *C(z) = new Counter;
is incorrect. Look at the left part of the statement. It represents itself a
definition of a variable. Variable name may not contain such symbols as
paranthesis. I.e. if you will drop the right part of the statement you will
get
Counter *C(z);
What is C(z)? Looks like a function prototype. It should be written as
Counter *C;
Futher, you want that your variable will be initialized. So you should write
Counter *C = new Counter(z);
However it is correct only if your class has a constructor which accepts an
integer as the first (if other parameters have default values) or only
parameter.

Vladimir Grigoriev
Vladimir Grigoriev
2008-05-15 14:09:54 UTC
Permalink
Or if you try to define a pointer to an array of objects of your class you
should write

Counter *C = new Counter[z];

Vladimir Grigoriev
Chris Uzdavinis (TeamB)
2008-05-15 15:07:47 UTC
Permalink
Post by John Scott
I can create an instance of a class, say, Counter for example, as follows.
Counter *C1 = new Counter.
and everything is fine.
However, I get a compiler error when I call a function and try to make more
than one instance. For example
SomeFunction( )
{
static int z = 0;
Counter *C(z) = new Counter;
}
Could anyone please explain what I am doing wrong or if there is
another way of achieving the same result.
You haven't said what you're trying to do. Clearly, when you ask for
another way of achieving the same result, a compile-time error is not
the result you want to achieve. (But there are many ways to
accomplish it!)

Unfortunately, the code you posted is too stripped to be of much use.
SomeFunction() doesn't even have a return type declared. If your
assignment compiled and succeeded, it would be immediately leaked and
unused. Static local integer 'z' is never modified, so why does it
exist? Not to mention, the main code in SomeFunction is syntactically
invalid so there is nothing to go by.

What *is* the actual intent? Are you expecting to pass z to the
new Conuter's constructor?

Through a *very* loose interpreation of your code and with much
creativity, I can also see a possible attempt at making an array of
objects, using z as the index. I hope that's not what you are trying
to do.... but if it is.... oh, dear.
--
Chris (TeamB);
Chris Uzdavinis (TeamB)
2008-05-15 15:11:02 UTC
Permalink
Post by Chris Uzdavinis (TeamB)
You haven't said what you're trying to do.
Also, after re-reading my post, I feel it may be worthwhile to mention
that I was being a little playful in the answer, and hope it didn't
come across as condescending or angry. Tone doesn't convey in email
too well.

Just say what you're trying to do and I'm sure we'll help you get
there. (Understanding why you're doing what you're doing might also
be helpful, as sometimes alternate approaches are superior, but only
considered when the context is understood.)
--
Chris (TeamB);
Liz Albin
2008-05-18 18:49:48 UTC
Permalink
Post by John Scott
I can create an instance of a class, say, Counter for example, as follows.
Counter *C1 = new Counter.
and everything is fine.
However, I get a compiler error when I call a function and try to
make more than one instance. For example
SomeFunction( )
{
static int z = 0;
Counter *C(z) = new Counter;
You're not makeing "more than one instance here" you're creating C
incorrectly. The syntax should be something like

Counter * c = new Counter(z);

where you've created a constructor that takes an int arguement. See
below
Post by John Scott
}
Could anyone please explain what I am doing wrong or if there is
another way of achieving the same result.
Many Thanks.
Try something like this:

class Counter
{

public:
Counter(int x) : dat_(x) {};
Counter() : dat_(0) {};
Counter(const Counter & c) : dat_(c.dat_) {};

private:

int dat_;
};


you could have a function like this

int f()
{
static int x;

Counter * c = new Counter(x);

// do stuff

delete c;

return x;
}
--
Liz

Please check the newsgroup guidelines and general netiquette

http://info.borland.com/newsgroups/guide.html
http://info.borland.com/newsgroups/guide.html#rules
http://info.borland.com/newsgroups/netiquette.html
Loading...