C object with () - what does it do? [duplicate] - c++

This question already has answers here:
What does A a() mean? [duplicate]
(2 answers)
Closed 9 years ago.
What does this particular piece of code do? To be more precise, what does test tob(); do?
class test {
private:
int a;
int b;
public:
test (int);
test();
};
test::test() {
cout<<"default";
}
test::test (int x=0) {
cout<<"default x=0";
}
int main() {
test tob();
}
I dont know what does test tob(); do, but it isn't giving any compilation errors.

test tob();
This declares a function which return type is test. It does not create an object. It's also know as most vexing parse.
To create a test object:
test tob;
Also, the way you define a function(include constuctor) with default argument is incorrect.
test::test (int x=0) { // incorrect. You should put it in function when it's first declared
cout<<"default x=0";
}
Below code should work:
class test {
int a;
int b;
public:
explicit test (int = 0); // default value goes here
};
test::test (int x) {
cout<<"default x=0";
}
int main() {
test tob; // define tob object
}

Related

Editing an instance of a class that has not yet been defined

Is it possible to edit an object that has not yet been defined in a function? For example, if I had a class in one file that adds one to a number:
class MyClass
{
private:
int i;
public:
MyClass()
{
i = 0;
}
int addOne()
{
i += 1;
return i;
}
};
And in another file, the class is imported. When the add function is called it calls the addOne function from the class:
#include "MyClass.h"
void add()
{
a.addOne();
}
int main()
{
MyClass a;
add();
return 0;
}
When I compile this it returns the error
error: ‘a’ was not declared in this scope
Is there any way for this to work?
Sites I read but didn't help solve my problem:
http://www.cplusplus.com/forum/unices/21527/
When can I use a forward declaration?
https://en.wikipedia.org/wiki/Forward_declaration
Can we have functions inside functions?
What you want to do is something like this :
void add( MyClass & a)
{
int num = a.addOne();
}
int main()
{
MyClass newObj;
// passing the new object to add
add(newObj);
return 0;
}
The method add() has no idea about the "a" which you were trying to use and was therefore giving you an error.
The method addOne() has a return type of an int, so it is expected that when you use a method that returns a value, then you do something with it.

What does v=0 in function parameters means [duplicate]

This question already has answers here:
assignment operator within function parameter C++
(3 answers)
Closed 5 years ago.
Here is a modified code from geeks for geeks.
#include<iostream>
using namespace std;
class Test {
int value;
public:
Test(int v = 0) {value = v;}
int getValue() {return value;}
};
int main() {
Test t(20);
cout<<t.getValue();
return 0;
}
What does parameter in function Test(int v=0) means?
That is default value for v parameter, which is the v variable will use that value if there is no value passed

function overloading and reading fdump-tree-all output [duplicate]

This question already has answers here:
C++ Overload Static Function with Non-Static Function
(5 answers)
Closed 7 years ago.
I was looking into a function overloading problem listed below and found that the following code doesn't compile.
#include<iostream>
class Test {
static void fun(int i) {}
void fun(int i) {}
};
int main()
{
Test t;
return 0;
}
My understanding was that member functions when compiled implicitly have an extra parameter, a pointer to the object in the compiled function. I am not sure what happens to the static functions. Now to figure out what the compiler was doing I tried running g++ -fdump-tree-all failed_overload.cxx and I got the files listed below:
failed_overload.cxx.001t.tu
failed_overload.cxx.002t.class
failed_overload.cxx.003t.original
failed_overload.cxx.004t.gimple
failed_overload.cxx.204t.statistics
I looked into gimple output and found the following:
**
static void Test::fun(int) (int i)
{
GIMPLE_NOP
}
void Test::fun(int) (struct Test * const this, int i)
{
GIMPLE_NOP
}
**
It seems like the static function just has the int parameter but the member function has the extra this parameter. If that is the case why is the compilation failing and why cant we overload the static function with the same signature.
If you had both static and non-static functions taking the same set of parameters, then in a call from a method of the class (non-static) it would be impossible to distinguish whether the programmer wants to call static or non-static function. Example:
#include<iostream>
class Test {
static void fun(int i) { std::cout << 2*i; }
void fun(int i) { std::cout << i; }
void otherFunc() {
fun(3); // Ambiguity: is static or non-static function intended?
}
};
int main()
{
Test t;
t.otherFunc();
return 0;
}

C++ define 3 arguments but using just 2 [duplicate]

This question already has answers here:
Default arguments in constructor--C++
(4 answers)
Closed 8 years ago.
I would like to use different number of arguments.
class A {
public:
A(int a, int b);
};
A::A(int a, int b) {
// constructor code
}
int main() {
A a(5); // I use only 1 argument and the second one I let default ?
}
Constructors are (a bit special) functions - regular default parameter syntax applies.
class A {
public:
A(int a, int b = default_value);
};
A::A(int a, int b) {
// constructor code
}
int main() {
A a(5);
}

What is wrong in this C++ code? Compile error: no matching function for call to ‘Test::Test(Test)’ [duplicate]

This question already has answers here:
Why is the copy-constructor argument const?
(8 answers)
Closed 9 years ago.
#include<iostream>
using namespace std;
class Test
{
/* Class data members */
public:
Test(Test &t) { /* Copy data members from t*/}
Test() { /* Initialize data members */ }
};
Test fun()
{
cout << "fun() Called\n";
Test t;
return t;
}
int main()
{
Test t1;
Test t2 = fun();
return 0;
}
What is wrong with above C++ code? compiler throws following error.
error: no matching function for call to ‘Test::Test(Test)’
You declared a copy constructor which requires a non-const lvalue. However, fun() returns a temporary and you can't bind a temporary to a non-const lvalue. You probably want to declare your copy constructor as
Test(Test const& t) { ... }