I have the following class:
class LineSegW {
public:
union {
struct {
public:
int sx;
int sy;
int ex;
int ey;
};
struct {
public:
Vec2i s;
Vec2i e;
};
};
int w;
LineSegW(int _sx, int _sy, int _ex, int _ey, int _w) :
sx(_sx), sy(_sy), ex(_ex), ey(_ey), w(_w) {}
};
Vec2i consists simply of 2 integers. Now the problem is that the given constructor does not initialise the class members, except the integer w which is not in the union. When I write the constructor as
LineSegW(int _sx, int _sy, int _ex, int _ey, int _w) {
sx = _sx;
sy = _sy;
ex = _ex;
ey = _ey;
w = _w;
}
everything works as expected. Both variants produce no warnings or errors so what is wrong with the first option?
Related
I made a struct as a data member in class. Now, I want to make a constructor to which I will pass my data members. How can I pass struct in constructor.
I wrote this code but it's not working.
#include <iostream>
#include <cmath>
using namespace std;
class circle
{
private:
struct center
{
int xcord;
int ycord;
};
struct center cordinate;
float radius;
public:
circle()
{
cordinate.xcord=0;
cordinate.ycord=0;
radius=0;
};
circle(int x, int y, float r) : cordinate.xcord(x), cordinate.ycord(y), radius(r)
{};
void showdata()
{
cout<<"Center(x,y) = ("<<cordinate.xcord<<","<<cordinate.ycord<<")"<<endl;
cout<<"Radius = "<<radius<<endl;
}
};
int main()
{
circle c1;
c1.showdata();
}
Or if I wrote it like this, it still don't work.
circle(int x, int y, float r) : center.xcord(x), center.ycord(y), radius(r)
{};
or
circle(int x, int y, float r) : center.cordinate.xcord(x), center.cordinate.ycord(y), radius(r)
{};
How can I pass struct to constructor?
You can initialize the cordinate member like this:
circle(int x, int y, float r) : cordinate{x, y}, radius{r} {}
Also, your default constructor can be simplified to:
circle() : circle(0, 0, 0) {}
which delegates to the 3-argument constructor. Even better, just use in class member initializers for all the fields:
struct center
{
int xcord = 0;
int ycord = 0;
} cordinate;
float radius = 0;
and then you can default the default constructor:
circle() = default;
Here's a demo.
I have a class which has a member attribute consisting of an object defined elsewhere. In the code below, A contains a public attribute var which is a B:
class B {
public:
int x, y;
std::vector<int> z;
B(int a, int b, std::vector<int> c) {
x = a; y = b; z = c;
}
};
class A {
public:
B var;
A(int i, int j) {
std::vector<int> someVector;
B(i, j, someVector);
}
};
int main() {
A foo(5, 3);
return 0;
}
This (obviously) doesn't compile as var is instantiated upon an instantiation of A, too late for it to be constructed.
The best way I can do something similar is modify some code:
class B {
public:
int x, y;
std::vector<int> z;
B() {}
void setAttributes(int a, int b, std::vector<int> c) {
x = a; y = b; z = c;
}
};
class A {
public:
B var;
A(int i, int j) {
std::vector<int> someVector;
B.setAttributes(i, j, someVector);
}
};
This does compile because attributes are set after instantiation.
But is there a way to remain closer to the first code snippet?
A(int i, int j) : var(i, j, {}) {}
Also, in your code B(i, j, someVector); does not initialize member variable var, and B.setAttributes(i, j, someVector); wouldn't compile at all.
if you cannot define a useful default constructor and don't want the ugly two step initialization, I guess there is no way around a pointer to B. Something like
#include <memory>
class B {
public:
int x, y;
std::vector<int> z;
B(int a, int b, std::vector<int> c) {
x = a; y = b; z = c;
}
};
class A {
public:
std::unique_ptr<B> var;
A() {
std::vector<int> someVector;
var = std::make_unique<B>(5, 2, someVector);
}
};
int main() {
A foo();
return 0;
}
should do the trick.
I have a such code:
struct Point
{
public:
int x;
int y;
Point(int xx, int yy) :x(xx), y(yy) {}
};
class figura
{
protected:
int x,y;
int cvet;
public:
virtual void Pokaz()=0;
};
class star:public figura
{
private:
int R;
int r;
Point *ar;
public:
star(int x1, int y2, int radc, int radk)
{
x = x1, y=y2, R = radc, r = radk;
ar = new Point[11];
}
};
What is with this line?
ar = new Point[11]; Error message is no matching function for call to Point::Point(). I don't know why, but it works on visual studio.
Is it possible to set multiple different class members in one statement? Just an example of how this would be done:
class Animal
{
public:
int x;
int y;
int z;
};
void main()
{
Animal anml;
anml = { x = 5, y = 10, z = 15 };
}
To "convert" Barry's comment into an answer, yes, under the conditions here:
An aggregate is an array or a class (clause 9) with no user-declared
constructors (12.1), no private or protected non-static data members
(clause 11), no base classes (clause 10), and no virtual functions
(10.3).
Example:
class Animal
{
public:
int x;
int y;
int z;
};
int main() {
Animal anml;
anml = { 5, 10, 15 };
return 0;
}
(This Community Wiki answer was added in accordance with this meta post.)
You can always overload constructors or create methods that "set multiple different object properties in one statement":
class Animal {
public:
Animal() {
};
Animal(int a, int b, int c) {
x = a;
y = b;
z = c;
}
void setMembers(int a, int b, int c) {
x = a;
y = b;
z = c;
}
private:
int x;
int y;
int z;
};
int main() {
// set x, y, z in one statement
Animal a(1, 2, 3);
// set x, y, z in one statement
a.setMembers(4, 5, 6);
return 0;
}
Solution 1 for Animal (http://ideone.com/N3RXXx)
#include <iostream>
class Animal
{
public:
int x;
int y;
int z;
Animal & setx(int v) { x = v; return *this;}
Animal & sety(int v) { y = v; return *this;}
Animal & setz(int v) { z = v; return *this;}
};
int main() {
Animal anml;
anml.setx(5).sety(6).setz(7);
std::cout << anml.x << ", " << anml.y << ", " << anml.z << std::endl;
return 0;
}
Solution 2 for any class with x, y (https://ideone.com/xIYqZY)
#include <iostream>
class Animal
{
public:
int x;
int y;
int z;
};
template<class T, class R> T& setx(T & obj, R x) { obj.x = x; return obj;}
template<class T, class R> T& sety(T & obj, R y) { obj.y = y; return obj;}
int main() {
Animal anml;
sety(setx(anml, 5), 6);
std::cout << anml.x << ", " << anml.y << std::endl;
return 0;
}
I'm having a bit of trouble with templating and composition-style coding. I have an object being created inside the constructor of another with the *this parameter. Sorry, if I'm being unclear. The code is as below:
In the outer.h file:
class outer {
public:
outer(int w, int l);
int getWidth();
int getLength();
private:
inner<outer> test(*this);
int width;
int length;
};
outer::outer(int w, int l) {
width = w;
length = l;
}
int outer::getLength() {
return length;
}
In the inner.h file
template<typename T>
class inner {
public:
inner(T &name);
private:
int top;
int bot;
};
template<typename T>
inner<T>::inner(T &name) {
top = name.getLength() /2;
bot = -name.getLength() / 2;
}
I don't know if this is allowed as I can't find anything online that addresses this. The compiler is having problems with the *this statement in outer.h.
Thanks in advance for your help.
If you're using C++03, you must perform initial assignments in the constructor.
class outer {
public:
outer(int w, int l);
int getWidth();
int getLength();
private:
// Member variables are initialized in the order they are declared here.
int width;
int length;
inner<outer> test;
};
outer::outer(int w, int l)
: width(w)
, length(l)
, test(*this)
{
}
Edit: Kerrek SB also observes that the order of your variables need to be changed. They are initialized in the order you declare them in the class and test needs to be initialized last, so the other variables are initialized.