Why doesn't class obj(); work?

Say you have a structure like so:
1
2
3
4
5
6
struct bob {
        int var;
	bob() {
		cout << "You've been initialized.";
	}
};


If you try to declare an object like so: bob obj();
That doesn't call the constructor, and more importantly, that isn't even an object because you can't access obj.var.

Then what is obj()! And why is it like this!?
Note that it compiles. Why does it compile?


edit: Is it being treated as a function prototype.. ;-;? I just realized.. Sorry.. stupid me.


But since we're here,
how does vector 5 in <int> foo(5); get considered as std::vector<int, std::allocator<int>>?

Also another question: Can you change the operator precedence for overloaded operators in a class?
And can you make your own operator - for a class (suppose I want # to be an operator to raise to the power of)?
Last edited on
What I meant was since 5 is of type int, how was it considered as allocator. What would happen if there was an int constructor? But I guess allocator can be any type because of template and since there's no type specific constructors for vectors it would work fine. And if there were type specific constructors then they would be used over the allocator because the template would never be called (correct me if I'm wrong).

Cool I didn't realize you could use template as constructor parameter.

Okay so I can't overload operators or change the precedence. That sucks. :'(
std::allocator is the default allocator, one can use their own, say for a memory pool.

Okay so I can't overload operators or change the precedence. That sucks. :'(


Consider taking more time to read the documentation properly. You are right about not being able to change precedence.
Consider taking more time to read the documentation properly. You are right about not being able to change precedence.


I didn't get the hint.. is 'consider taking more time to read the documentation properly' referring to the statement 'Okay so I can't overload operators or change the precedence'?

Then can you be more specific.. do you mean to say that it's partially correct but not fully? Or what did you mean to say? I don't get it..
Then can you be more specific..


Well you have some nerve to tell me to be more specific. I provided a link, you just need to read it properly.
> how does std::vector<int> foo(5); get considered as std::vector<int, std::allocator<int> >?

Template: template < typename T, typename Allocator = std::allocator<T> > > class vector;
In std::vector<int> foo(5); the second template argument (for the allocator) is defaulted,
so the type of foo is std::vector< int, std::allocator<int> >

foo(5) - constructor: explicit vector( size_type count, const Allocator& alloc = Allocator() );
https://en.cppreference.com/w/cpp/container/vector/vector (3)

Construct the vector with
a. 5 default inserted values of type int and
b. a default constructed allocator of type std::allocator<int>
Well you have some nerve to tell me to be more specific. I provided a link, you just need to read it properly.


Okay, how about you go run off a cliff? I won't tell you why. Don't you dare ask me to be more specific. I provided you instructions, go perform it.

Do you understand how you sound?
Topic archived. No new replies allowed.