difference btw two type of declaration

1
2
3
4
5
6
7
8
9
10
Class Base
{

};

main()
{
  Base b;        // First type
  Base b();      // Second type
}



What is the difference in above two type of declaration?
There's no difference. On creating the objects you are calling the default constructor.

Edit: That's wrong -.-*
Last edited on
hi kunalnandi,

First type:
(creating object which will be initialized with default constructor)


Second type:
(declaration of an function which takes no argumets and returns an object of class "Base")
#note that declaration not definition!

keyword class must begin with small 'c'
your code:
1
2
3
4
Class Base //REPLACE WITH class
{

};


EDIT:
main function must have function type and return value as all other functions do...
your code:
1
2
3
4
5
6
main()  //ADD int
{
  Base b;   
  Base b();    
//ADD return 0;
}
Last edited on
Hello codekiddy,

but the second type is an instantiation, too (at least in this context).

Edit: That's wrong -.-*
Last edited on
hi shadow123,

yes you may be right by saying context, but in context of the code which mr. threadstarter provided you're not.

because an empty parentheses initializer is allowed in other contexts e.g. in a new expression or constructing a value-initialized temporary.

for example:
1
2
3
4
5
int main() {
   Base* a = new Base();
   Base b = Base();
   return 0;
}


cheers mate!
Last edited on
hi

first one is the declaration of an object by default constructor. (google it).

second is the declaration of the object with explicit constructor. In this case you usually pass a value right away which becomes a value of the element of the class;

For ex:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Class Base
{
public:
Base();
Base(int x);

private:
int number;
};

Base::Base(){
number = 0;
}

Base::Base(int  x){
number = x;
}

main()
{
  Base b;        // First type. in this case NUMBER is equal to 0
  Base b(60);      // Second type. in this case NUMBER is equal to 60 but you have to pass a value to the object when you create it.
}


Something like that if I got you question correctly.
closed account (1vRz3TCk)

1
2
3
4
5
6
7
8
9
10
Class Base
{

};

main()
{
  Base b;        // First type
  Base b();      // Second type
}


What is the difference in above two type of declaration?


The first declares an object called b that is of type Base.
The second declares a function called b that returns a Base.

See: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.2
Something like that if I got you question correctly.

hi Karajic, don't get me wrong but /
I think the question was:
1
2
 Base b;        // First type
 Base b();      // Second type 

and not:
1
2
Base b;          // First type
Base b(60);      // Second type. 


The question was using default constructor, not overloaded(user defined) constructor.

kind regards!
@CodeMonkey
The first declares an object called b that is of type Base.
The second declares a function called b that returns a Base.

Respect, but the same answer has already been given ;)
closed account (1vRz3TCk)
Respect, but the same answer has already been given ;)

I know but it was disputed. A second answer with corroborating information only goes to reenforce the correct answer.
Ooops, I've compiled/tested the wrong example :(

Sure, codekiddy is right!
Thank you everyone for your answers.

1
2
 Base b;        // First type
 Base b();      // Second type 


What I understand is there can be two type of context in these two type of declaration

First Context:

If you want to declare a object of class Base there can be either on declaration at a time, because both of them are same kind of declaration.

Second Context:

Create a object of class Base and if you want to declare a function which returns object of Base class then you must use different name it can't be Base b();, if you don't do so compiler will throw error that you have done a redeclaration of object b
hi,

object can be created in few different ways:

int a; (object created on the stack and it's initial value is 0)
int* a = new int; (object created dinamicaly and it's initial value is unknown)

Base a = Base(); (here right operand is temporal object and it's initial value(of it's members) is 0)
Base* b = new Base; (here b is initialize with value of zerro) (it's members!)
base* c = new Base(); (here c object (not pointer ) is initilized with zerro cos default constructor has been cled)

Base x(); (THIS IS WRONG AND HAS NO EFECT) just a sily declaration of some function

of course is has efect if you realy have such function defined(not declared)

as you see the is no context but just a simple rules about declaration and mermory alocation.

cheers!
Last edited on
Thanks!!
Topic archived. No new replies allowed.