470,855 Members | 1,248 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

Creating a class

In the main below I create a Swat class in three ways:
class Swat {
public:
Swat(){
n = 0;
}
Swat(int a){
n = a;
}
int getS() {
return n;
}
void setS(int b) {
n = b;
}
private:
int n;

};

int main() {

// First
Swat ss1;
ss1.setS(33);

// Second
Swat ss2();
ss2().setS(22); // Illegal!
// Third
Swat* ss3 = new Swat();
ss3->setS(55);
printf("%d\n", ss1.getS());
printf("%d\n", ss3->getS());

return 0;
}
Only the First and Third class creation seems to make sense. My question
is:

1) When are // Second used?

2) When would // Third be necessary, seems that // First does the job just
fine.

3) Are there any other ways to create a class besides the 3 above?
Jun 27 '08 #1
4 4882
saneman wrote:
In the main below I create a Swat class in three ways:
class Swat {
public:
Swat(){
n = 0;
}
Swat(int a){
n = a;
}
int getS() {
return n;
}
void setS(int b) {
n = b;
}
private:
int n;

};

int main() {

// First
Swat ss1;
ss1.setS(33);

// Second
Swat ss2();
That's a declaration of a function.
ss2().setS(22); // Illegal!
Illegal? Really? What's illegal about it? Did you make a typo and in
your code you actually *don't* have any parentheses after 'ss2'?
>

// Third
Swat* ss3 = new Swat();
ss3->setS(55);
printf("%d\n", ss1.getS());
printf("%d\n", ss3->getS());

return 0;
}
Only the First and Third class creation seems to make sense. My question
is:

1) When are // Second used?
When you want to declare a function.
>
2) When would // Third be necessary, seems that // First does the job just
fine.
When you want your object to outlive the scope.
>
3) Are there any other ways to create a class besides the 3 above?
3? Not 3, only 2. 'ss1' has automatic storage duration. 'ss2' is a
function (and not an object). 'ss3' has dynamic storage duration. If
you want static storage duration, you can either place the object's
declaration outside of any function or add the keyword 'static' to the
declaration.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
"saneman" <as*@ad.comwrites:
In the main below I create a Swat class in three ways:
class Swat {
public:
Swat(){
n = 0;
}
Swat(int a){
n = a;
}
int getS() {
return n;
}
void setS(int b) {
n = b;
}
private:
int n;

};

int main() {

// First
Swat ss1;
ss1.setS(33);

// Second
Swat ss2();
ss2().setS(22); // Illegal!
// Third
Swat* ss3 = new Swat();
ss3->setS(55);
printf("%d\n", ss1.getS());
printf("%d\n", ss3->getS());

return 0;
}
Only the First and Third class creation seems to make sense. My question
is:
No, you didn't create a class thrice. You created the class Swat only
once (have the compiler create it actually). What you did, at
run-time, is to create TWO instances of the class Swat, TWO objects,
and declared ONE function that returns an instance of Swat.

Both lines:

Swat ss2();
ss2().setS(22);

are perfectly legal, but they don't do what you believe.

The first one declares a function named ss2 taking no argument and
returning an instance of Swat.

The second one calls this function ss2, and sends the message setS(22)
to the returned object.

Since you defined no function named ss2, the link edition will fail.
If you define the function ss2, it works perfectly well:
-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Mon Jun 16 18:02:52

cd /tmp ; g++ -o c c.c++ && ( echo -------------------- ; cat c.c++ ; echo -------------------- ; ./c )
--------------------
#include <iostream>

class Swat {
public:
Swat(){
n = 0;
}
Swat(int a){
n = a;
}
int getS() {
return n;
}
void setS(int b) {
n = b;
}
private:
int n;

};

Swat ss2(){
return Swat(3);
}

int main() {

// First
Swat ss1;
ss1.setS(33);

// Second
Swat ss2();
ss2().setS(22);
// Third
Swat* ss3 = new Swat();
ss3->setS(55);
std::cout<<ss1.getS()<<std::endl;
std::cout<<ss3->getS()<<std::endl;

return 0;
}

--------------------
33
55

Compilation finished at Mon Jun 16 18:02:52

1) When are // Second used?
When you want to initialize an automatic object with some parameters.
For example, you could write:

// Second
Swat ss2(42);
ss2.setS(22);

2) When would // Third be necessary, seems that // First does the job just
fine.
The difference is that the first object will be deleted when the
function main exits. The third object life is not limited by the
function that created it, it can outlive it.
3) Are there any other ways to create a class besides the 3 above?
No, in C++, there is no (practical or portable) way to create a class
at run-time.
There are other ways to create objects, or temporary objects, using
copy-constructors:

Swat ss4=Swat(42);

This creates a temporary object (by Swat(42)) and then copies it into
ss4, and then delete the temporary object. Of course, since this is
ridiculous enough, the compilers can optimize it and just do the same
as with:

Swat ss2(42);
Also, writting:

Swat ss2(){
return Swat(3);
}

is simiar to ss4=Swat(42); it creates a temporary object, returning it
as result of the function.

--
__Pascal Bourguignon__
Jun 27 '08 #3
On Jun 16, 5:54 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
saneman wrote:
// Second
Swat ss2();
That's a declaration of a function.
ss2().setS(22); // Illegal!
Illegal? Really? What's illegal about it?
He hasn't provided an implementation of ss2. So it's undefined
behavior. (In practice, he'll get an error message from the
linker.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #4
James Kanze wrote:
On Jun 16, 5:54 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>saneman wrote:
>>// Second
Swat ss2();
>That's a declaration of a function.
>>ss2().setS(22); // Illegal!
>Illegal? Really? What's illegal about it?

He hasn't provided an implementation of ss2. So it's undefined
behavior. (In practice, he'll get an error message from the
linker.)
When I ask my son (who is almost 2) what color the ball he's holding is,
I expect my daughter (who is almost 5) to keep silent. I keep telling
my daughter to hold her answer because I need my son to apply his brain.
I did not think I would need to do the same here. The question was
for the OP. He didn't provide any substance to the "Illegal!" claim.
Next time, James, when you encounter a question from me in a situation
like this, perhaps you would consider this. Thanks!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Edvard Majakari | last post: by
6 posts views Thread by Davinci_Jeremie | last post: by
15 posts views Thread by Carlos Lozano | last post: by
8 posts views Thread by Simon Edwards | last post: by
12 posts views Thread by Mats Lycken | last post: by
17 posts views Thread by Lee Harr | last post: by
6 posts views Thread by wcc | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.