Creating a class

S

saneman

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?
 
P

Pascal J. Bourguignon

saneman said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top