Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a simple yet maybe stupid question. I was asked to point out the difference between two initializing ways. I am not really able to point out the difference. Could you help me?
A(int a);
A(int a=3);
Assuming A is a class or struct, then:
A(int a);
defines a converting constructor that requires the caller to provide an input value when creating an object instance of A, eg:
A a(12345); // OK
A a; // ERROR
Whereas:
A(int a=3);
defines a converting constructor that has a default parameter value, allowing the caller to omit passing in a value if desired, eg:
A a1(12345); // OK, will use a=12345
A a2; // OK, will use a=3
Since passing in an input value is optional in this constructor, it can thus also serve as A's default constructor as well, unless one is also defined, then this becomes ambiguous as to which constructor to call when no input value is provided, eg:
A();
A(int a = 3);
A a; // ERROR, ambiguity
In the first one you must provide the constructor an int.
A(int a);
In the second you can provide an int, but if you don't the default value for a will be 3.
A(int a = 3);
A(int a);
A(int a=3);
We can't declare the above constructor declaration together in the class.
Because compiler will say function already defined.
A(int a); is equal to A(int a=3);
For example if you have one constructor like as follows
A(int value=3) { a = value; cout << "\nOne Parameter Constructor" << " a="<<a; }
Object Creation:
A a1; A a2(6);
Output:
One Parameter Constructor a=3
One Parameter Constructor a=6
Related
This question already has answers here:
Implicit constructor argument conversion in C++11
(3 answers)
Closed 3 years ago.
class A{
public:
A(int i){}
};
class B{
public:
B(A){}
};
void test(B){};
int main() {
A a=1;
B b1=a;
test(1);
}
I have two classes, where the first class has a converting int Ctor, which means that I can construct it by "assigning" an integer (first line).
The second class has a converting Ctor with argument type A. It is possible to construct b1 with a (second line).
Is there any possibility to use multiple converting ctor in a chain, in such a way that the third row will compile?
You can explicitly construct B from int, but not implicitly:
test(B{1}); // ok
test(1); // Not ok
That being said, this half-way measure also works:
test({1}); // ok
And everything above works for only one implicitly generated middle class (A). Such a chain can not be further extended.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to do a bit of refactoring and I am curious about how would you approach this problem.
Basically I'm trying to create an initialization function for each class. There are classes that inherit from some others, and i would like to use parent initialization function if possible. How would you address this?
I would like to use these structs with memcpy and maybe using also them with the keywords align and __attribute__((packed)); and they must be usable with extern "C". I would exclude then constructors and destructors.
An example to explain:
struct A
{
int a;
};
void initialize(A& a)
{
a = 0;
}
struct B : A
{
int b;
};
void initialize(B& b)
{
initialize(b); // here I want void initialize(A& a), not recursion
b = 0;
};
Maybe I have to do some kind of cast? Ideally I'm looking a solution that does not create overhead.
Use a static_cast.
In your code, the initialize(b) call will recurse infinitely, because b is better matched as B& than as A& (the argument of the function you want to call), thus the overload resolution picks the same function and recurs.
You specified that you want to initialise the A part of the b object. Why not tell that to the compiler? Tell it that you want to call initialise in it as though it was an A, like so:
initialize(static_cast<A&>(b));
As for your concern that you mentioned in the comment - no copies are being made here. If I used static_cast<A>, however, a temporary object would be created, but that's not the case. I am not casting b to an object of a type A. I am casting it to a reference of a type A, which will result in creation of temporary reference. Since A& matches with A& better than with B&, the first function will be chosen, thus avoiding the recursion.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
#include <iostream>
using namespace std;
class dummy
{
private:
int a,b,*p;
public:
void setdata(int x,int y,int z)
{
a=x;
b=y;
p=&z;
}
void showdata()
{
cout<<"a "<<a<<"b "<<b<<" pointer address c "<<&p<<endl;
}
dummy(dummy &d)
{
a=d.a;
b=d.b;
p=d.p;
cout<<"a "<<a<<"b "<<b<<" pointer address c "<<&p<<endl;
}
dummy(dummy &d)
{
d.a;b=d.b;p=d.p;
}
};
int main()
{
dummy d1;//error is here;
d1.setdata(3,4,5);
dummy d2=d1;
d2.showdata();
d1.showdata();
return 0;
}
raised error
:/root/copy deep shallow/main.cpp|15|error: no matching function for call to ‘dummy::dummy()’|
I am unable to understand why the error messege is raised and what is the solution for this issue
The class does not have the default constructor because there is the explicitly defined copy constructor
dummy(dummy &d){a=d.a;b=d.b;p=d.p;}
(that shouuld be declared with the parameter const dummy &)
However in this declaration
dummy d1;
there is needed the default constructor that is absent.
You have explicitly to define the default constructor.
Take into account that for example this member function
void setdata(int x,int y,int z)
{a=x;b=y;p=&z;}
leads to undefined behavior because the pointer p will have an invalid value after exiting the function because the local variable (parameter) z will be destroyed.
dummy()
{
}
dummy(dummy &d)
{
a=d.a;
b=d.b;
p=d.p;
cout<<"a "<<a<<"b "<<b<<" pointer address c "<<&p<<endl;
}
This will fix your issue.
In this way you define a default constructor (1st) and a copy-constructor (2nd)
In your code you provided no default constructor and two copy constructor, so compiler will complain about the absence of constructor first, but when will you'll solve that it will raise another error due to ambiguos copy constructors (compiler will tell you I don't know which one call).
As third error, setData() will lead to undefined behaviour of p being invalid after destruction of z.
Your problem is not with copy-constructor, but with default constructor.
Whenever you write a class in C++, the compiler will generate a set of default functions, so you don't have to type them explicitly. Those are:
Default constructor: dummy::dummy()
Copy constructor: dummy::dummy(const dummy&)
Copy assignment operator: dummy& dummy::operator=(const dummy&)
Destructor: dummy::~dummy()
In C++11 standard you also get move constructor and move assignment operator, but these are not important here.
Compiler adds these to any class for you, but if you define any constructor, it won't generate implicit default constructor.
If you want to have default constructor (and be able to use dummy d1; in code), add the following constructor definition to the class:
dummy(){}
or better, if you use C++11 standard:
dummy() = default;
Side note: you don't need explicit copy constructor in this case. Your implementation simply copies content of every field to new object, and thus implicit copy constructor (generated by compiler) would be enough.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a class that creates instances of other classes, and when I call them, compiler gives me warning about order of the instances. Why does it matter? It does same job, regardless of the order.
E.g. I have this in my core class header file (core class handles game loop):
HUD hud;
World myWorld;
Like this they do all they need to. But compiler gives a warning:
'Core::myWorld' will be initialized after [-Wreorder]|
Then if I put myWorld instance above the hud instance, it doesn't give me warning anymore. I was just wondering, how on earth does it matter which order they are in?
Warning is since, in constructor initializer-list you initialize World before HUD, but actually members will be initialized in order they are declared in class.
Just litle example, where it can be worse:
class B
{
public:
B(int i) : value(i) {}
private:
int value;
};
class A
{
public:
A() : value(10), b(value)
{
}
private:
B b;
int value;
};
Here b will be initialized before value and so, something will be sended to b constructor, but not 10 as programmer want.
With
struct C
{
C() : p(std::make_unique<int>(42)), j(*p) {} // initialization doesn't use this order.
// but the order here:
int j;
std::unique_ptr<int> p;
};
you will dereference nullptr as j is initialized before p.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this chuck of code and I'd really appreciate if someone could help me out by giving some more information about what it is. I know a bit of coding, but not too much so please bear with me if this seems silly.
class A
{
public:
A();
};
int x, y, z;
A::A():
x(1),
y(2),
z(3)
{
//some code here basically more variable definition
}
What I don't get is this,
a function which is also the name of a class? Even if it works, but why would I want to do that?
what is up with the variable definition of x, y, z. I assume some constructors are being called, but what's wrong with defining them the normal way?
And what does do they being after the colon : but before the function definition signify?
Thank you. Much appreciated.
Really, a C++ tutorial is what you need, but here goes:
A "function" with the same name as the class is a constructor. There can be several of these, each taking different arguments. In this case, the constructor takes no arguments at all (and so is called a default constructor). Not all classes have a default constructor, but if you don't write any constructors of your own, then the compiler will create a default constructor for you.
The bit after the colon is a special bit of syntax used in constructors. It's called an initializer list. What happens is that before you get to the body of the constructor, the member variables listed are initialized with those values (or, in the case of objects, constructors are called with those arguments). You should always use an initializer list to set the values of variables if you can.
In this case then, the default constructor specifies that the member variables x, y and z should be initialized to 1, 2 and 3 respectively (at least I assume they're meant to be member variables -- as written, they're actually globals).
What you have in your question isn't valid C++.
class A
{
public:
A();
};
int x,y,z; // x,y,z are out here in the wild
// This is supposed to be the definition of the constructor of "A"
// outside the "A" class
//
// The "A::A() : " sequence introduces a list of member initializers.
A::A() :
x(1), // <- These are not members of "A", so they cannot be initialized as members!
y(2), // <-
z(3) // <-
{
}
What I think you meant was:
class A
{
public:
int x,y,z;
A();
};
A::A() :
x(1), // <- Now members of "A", so they can be initialized here
y(2), // <-
z(3) // <-
{
}