hello all,
is this a proper way of declaring a object
thax very muchCode:someclass obj();
s.s.harish
hello all,
is this a proper way of declaring a object
thax very muchCode:someclass obj();
s.s.harish
Nope. Thats a function declaration of a function called obj that takes no params and returns a someclass by value.
Lose the parentheses and you have what you want.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
thax Stoned_Coder for your reply
i mean something like this
s.s.harishCode:class someclass { public: someclass(int); someclass(); void dosomething(); private: int num; }; ////////////////// class someclass implememtation/////////////////////// someclass::someclass(int val):num(val) { } someclass::someclass():num(0) { } void someclass::dosomething() { cout<<"The value of num is "<<num<<endl; } //////////////main function////////////////////// int main() { someclass obj1(20); someclass obj2(); // when i say this it compiles fine with no error. obj1.dosomething(); obj2.dosomething(); // when i take off this statment it compiler fine cin.get(); }
of course it compiles, its legal. It just doesnt do what you want. To call a default constructor you just do this...
someclass obj;
This declares an object of someclass and calls the default constructor.
someclass obj();
This on the other hand as explained above is a function declaration.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi