correct me please

This is a discussion on correct me please within the C++ Programming forums, part of the General Programming Boards category; hello all, is this a proper way of declaring a object Code: someclass obj(); thax very much s.s.harish...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    181

    Question correct me please

    hello all,
    is this a proper way of declaring a object

    Code:
    someclass obj();
    thax very much

    s.s.harish

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    thax Stoned_Coder for your reply

    i mean something like this

    Code:
    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();
    }
    s.s.harish

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux for GNU/Linux is not correct?
    By password636 in forum Linux Programming
    Replies: 8
    Last Post: 03-31-2009, 09:30 PM
  2. Is this correct : passing strings?
    By socket in forum C Programming
    Replies: 15
    Last Post: 11-25-2008, 02:03 PM
  3. correct order to insert new node?
    By campermama in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2004, 08:51 PM
  4. Replies: 1
    Last Post: 05-26-2004, 01:58 AM
  5. Loop until enter correct value
    By jchanwh in forum C Programming
    Replies: 2
    Last Post: 11-27-2001, 01:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21