![]() |
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? |
Re: Creating a class
"saneman" <asd@ad.com> 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__ |
Re: Creating a class
On Jun 16, 5:54 pm, Victor Bazarov <v.Abaza...@comAcast.net> 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:james.kanze@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 |
| All times are GMT. The time now is 12:36 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.