Why Constructor is not Calling with Function call Operators [duplicate] - c++

This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 7 years ago.
Sorry I was unable to find any keyword to search for this problem. My code is below
#include <iostream>
using namespace std;
class Name {
public:
Name() {
cout << "Asif";
}
};
int main() {
Name obj(); // why Constructor not calling here?
return 0;
}
If it's not calling the constructor then what process is running in this code?

Name obj(); // why Constructor not calling here?
This is a function declaration, returning a Name object
Just do
Name obj;

Related

Can we pass thread to parameterised constructor that accepts argument by rvalue reference? [duplicate]

This question already has answers here:
Most vexing parse C++11
(2 answers)
What is the purpose of the Most Vexing Parse?
(6 answers)
Is most vexing parse a formally defined concept
(2 answers)
Closed 4 months ago.
I would like to pass std::thread temporary object directly to my ThreadGuard class but somehow that is not producing any output, however if I first create local std::thread variable and pass that to constructor then it is working fine. So my question is can't we pass temporary thread to function taking rvalue reference parameter ?
#include <iostream>
#include <thread>
using namespace std;
class ThreadGuard
{
private:
std::thread& m_t;
public:
ThreadGuard(std::thread& t):m_t(t)
{
std::cout<<"By lvalue ref constructor "<<std::endl;
}
ThreadGuard(std::thread&& t):m_t(t)
{
std::cout<<"By rvalue ref constructor "<<std::endl;
}
~ThreadGuard()
{
if(m_t.joinable())
{
m_t.join();
}
}
};
void foo()
{
std::cout<<"Inside foo..."<<std::endl;
}
int main()
{
//This is working
//std::thread th(foo);
//ThreadGuard t1(th);
//This is not giving any output
ThreadGuard t(std::thread(foo));
return 0;
}
What you have just come across is C++'s most vexing parse syntax.
The compiler thinks that you have declared a function t which returns a ThreadGuard and takes an argument foo of type std::thread:
ThreadGuard t(std::thread foo);
A possible solution would be to use curly braces for initialization, as such:
ThreadGuard t(std::thread{foo});

classname classobject(); does not invoke any constructor [duplicate]

This question already has answers here:
My attempt at value initialization is interpreted as a function declaration, and why doesn't A a(()); solve it?
(5 answers)
Closed 3 years ago.
If I have a following situation:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "Inside A" << endl;
}
};
int main() {
A a();
return 0;
}
Why is the constructor not invoked?
If something looks like a function declaration, the C++ standard requires it be treated as a function declaration.
A a(); does not default-construct an object a of type A. It declares a function a that takes no input parameters and returns an A object as output.
To default-construct a variable a, you need to drop the parenthesis:
A a;
Or, in C++11 and later, you can use curly braces instead of parenthesis:
A a{};

C++ Classname Function() [duplicate]

This question already has answers here:
error: request for member '..' in '..' which is of non-class type
(9 answers)
Closed 4 years ago.
I was recently giving an online quiz on c++ and a question came that had similar syntax as
class className
{
public:
constructor()
{
print("ABC");
}
}
int main()
{
className ABC();
return 0;
}
I thought it would not compile but rather it compiled and ran without having any effect, I am interested as to what feature this is and in which case do we use this?
Your posted code won't compile. Ignoring that there are 2 reasons it doesn't do anything.
className ABC(); declares a function. To call the default constructor of a class simply omit the brackets: className ABC;
className has a method called constructor but no contructor so doesn't print anything when it is constructed.

Which constructor will be called? [duplicate]

This question already has answers here:
Default constructor with empty brackets
(9 answers)
What does X f() mean?
(4 answers)
Closed 4 years ago.
Why there is no output?
Why doesn’t an ambiguity error occur?
#include <iostream>
using namespace std;
class A{
public:
A(){
cout << "Class A";
}
A(int a = 0){
cout << "A";
}
};
int main() {
A a();
return 0;
}
So when we create an object “a” of type “A” A a();, compiler calls a constructor without parameters, doesn’t he?
In C++, We can have more than one constructor in a class with same name, as long as each has a different list of arguments. A constructor is called depending upon the number and type of arguments passed.
While creating the object, arguments must be passed to let compiler know, which constructor needs to be called.

constructor resolution, default constructors and () [duplicate]

This question already has answers here:
My attempt at value initialization is interpreted as a function declaration, and why doesn't A a(()); solve it?
(5 answers)
Closed 5 years ago.
#include <iostream>
using namespace std;
class A{
public:
A(){
cout << "Class A!";}
};
int main()
{
A a();
}
the above code does not call the constructor A :: A() even though they have the same input parameters (none). however if, in the main function, I remove the parenthesis from
A a();
it calls the constructor.
so what is the difference between A a; and A a();
i believe the question here is very similar maybe even the same however if some could explain in simpler terms I would be very grateful.
Do the parentheses after the type name make a difference with new?
would declaring A a(); ever call a constructor, under any circumstances?
do parameterless constructors exist in c++, or is that the same as a default constructor?
With
A a();
you declare a as a function taking no arguments and returning an A object.