Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Creating a class

Reply
Thread Tools

Creating a class

 
 
saneman
Guest
Posts: n/a
 
      06-16-2008
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?


 
Reply With Quote
 
 
 
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      06-16-2008
"saneman" <(E-Mail Removed)> writes:

> 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__
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      06-16-2008
On Jun 16, 5:54 pm, Victor Bazarov <(E-Mail Removed)> wrote:
> 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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using std::map in a template class, getting LNK2019 link error when creating an object from this template class girays C++ 1 01-20-2007 09:47 PM
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Creating inherited class with set base class Josh Mcfarlane C++ 2 08-18-2005 05:31 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM



Advertisments