I have an object of class A.
class A[
{
int x;
string y;
float z;
....
}
Then I have an int, called "integer".
How can I redefine the = operator in order to do something like
int integer;
A obj = integer;
in order to obtain something equal to the constructor call with NOT all members:
A obj(integer,",0);
This is a little naughty, but:
#include <iostream>
using std::cout;
using std::endl;
class A {
public:
int x;
A & operator=(int value) {
x = value;
return *this;
}
};
int main(int, char **) {
A obj;
obj.x = 5;
cout << "Initially: " << obj.x << endl;
obj = 10;
cout << "After: " << obj.x << endl;
}
When run:
g++ Foo.cpp -o Foo && Foo
Initially: 5
After: 10
Is this what you're trying to do? Note that this is very naughty. class A is NOT an integer, and assigning it to an int is going to confuse people. C++ lets you do things that you probably shouldn't do, and this is one of them.
My program deals with accessing attributes of a structure which is defined inside a class. When I use a pointer of type struct to show the structure's attributes, it is throwing a segmentation fault and stops the execution. Though it is printing the structure's attributes fine if I just use a variable of type struct. I tried debugging the code with gdb and it is showing the segmentation fault occurs at line 27 which is poly.params->a = 1;. Why can't we use pointers in this case or am I making a stupid mistake? Here is the sample of the code:
#include <iostream>
using namespace std;
class QuadraticFunc
{
public:
QuadraticFunc(){};
struct Coeff
{
double a;
double b;
double c;
} * params;
void ParamShow(const Coeff *params)
{
cout << "a: " << params->a << endl;
cout << "b: " << params->b << endl;
cout << "c: " << params->c << endl;
}
~QuadraticFunc(){};
};
int main()
{
QuadraticFunc poly;
poly.params->a = 1;
poly.params->b = 2;
poly.params->c = 1;
QuadraticFunc *polyPtr;
polyPtr = &poly;
cout << "The parameters for the first object: " << endl;
polyPtr->ParamShow(poly.params);
}
poly.params->a = 1;
params has not been initialized.
Replace
struct Coeff
{
double a;
double b;
double c;
} * params;
with
struct Coeff
{
double a;
double b;
double c;
} params;
And then replace each params-> with params.
Why can't we use pointers in this case or am I making a stupid mistake?
Yes, you are.
QuadraticFunc poly; // Your constructor leaves poly.params uninitialized
poly.params->a = 1; // Dereferencing uninitialized pointer invokes undefined behavior.
How should I fix this?
The best solution is to avoid the pointer here:
class QuadraticFunc
{
public:
QuadraticFunc(){};
struct Coeff
{
double a;
double b;
double c;
} params;
...
};
int main()
{
QuadraticFunc poly;
poly.params.a = 1;
...
polyPtr->ParamShow(&poly.params);
}
You may have a legitimate reason to have params as a pointer, but you have not yet shown what that reason may be.
Let's imagine situation:
#include <iostream>
int d =34;
namespace
{
int d =45;
}
int main()
{
std::cout << ::d ;
return 0;
}
Here the output is 34, because :: means global namespace. But If I comment 3rd line the output is 45, which is strange.
If I use std::cout << d ; - I get error
s.cxx:12:15: error: reference to ‘d’ is ambiguous
How can I access unnamed_namespace::d in this scenario?
PS: I've read that unnamed namespace is used for static global variables aka visible only in file scope
You cannot disambiguate between the two ds in main without the aid of something else.
One way to disambiguate between the two is to create a reference variable in the namespace and then use the reference variable in main.
#include <iostream>
int d = 34;
namespace
{
int d = 45;
int& dref = d;
}
int main()
{
std::cout << dref << std::endl;
return 0;
}
But then, why confuse yourself with the same variable? If you have the option, use a different variable name in the namespace or give the namespace a name.
namespace
{
int dLocal = 45;
}
int main()
{
std::cout << dLocal << std::endl;
std::cout << d << std::endl;
return 0;
}
or
namespace main_detail
{
int d = 45;
}
int main()
{
std::cout << main_detail::d << std::endl;
std::cout << d << std::endl;
return 0;
}
I'm new to C++ and could not figure out how can I define a variable that holds 3 values,
e.g. coordinates hold 2 values, as (x,y).
I tried:
typedef int U_K(int a,int b,int c);
but that doesn't seem to work.
I'd really appreciate a quick simple answer :)
Thanks!
edit:
So i did this :
struct U_K{
float a,b,c;
};
U_K Uk; //this line
is this wrong? because i get "unknown type name U_K" for that line... i first though its because i needed to declare it under the function i am going to use the struct for, but turns out there is the error for both cases.
the shortest way is to use a struct
struct U_K
{
int a,b,c;
};
usage:
U_K tmp;
tmp.a = 0;
tmp.b = 1;
tmp.c = 2;
You can add complexity to that type by adding member function/constructors to make the usage of U_K easier:
struct U_K
{
int a,b,c;
U_K() //default constructor
:a(0)
,b(0)
,c(0)
{}
U_K(int _a_value,int _b_value, int _c_value) //constructor with custom values
:a(_a_value)
,b(_b_value)
,c(_c_value)
{}
};
//usage:
int main()
{
U_K tmp(0,1,2);
std::cout << "a = " << tmp.a << std::endl;//print a
std::cout << "b = " << tmp.b << std::endl;//print b
std::cout << "c = " << tmp.c << std::endl;//print c
}
Alternatively you can use std::tuple to obtain the same result. Using it is different:
std::tuple<int,int,int> t = std::make_tuple(0,1,2);
std::cout << "a = " << std::get<0>(t) << std::endl;//print first member
std::cout << "b = " << std::get<1>(t) << std::endl;//print second member
std::cout << "c = " << std::get<2>(t) << std::endl;//print third member
If you are learning c++ now you should know that the implementation std::tuple is much more complex than a trivial struct and to understand it you need to learn about templates and variadic templates.
struct TypeWith3Ints
{
public:
int a;
int b;
int c;
};
Use std::array<int, 3> which is to be preferred over tuple in this case as a homogenous container can be used.
If you want to typedef:
#include <array>
typedef std::array<int, 3> X;
Use std::tuple, then you don't have to make your own structure. Just write
std::tuple<int, int, int> your_tuple(a,b,c);
std::cout << std::get<1>(your_tuple) << ' '; // b
your_tuple = std::make_tuple(c,d,e);
std::cout << std::get<0>(your_tuple) << ' '; // c
If you want your own name, use alias, like:
typedef std::tuple<int, int, int> your_name;
your_name your_object(a,b,c); //your_tuple
std::cout << std::get<2>(your_tuple) << ' '; // c
If you want, your own structure, and if you want to write object(x,y,z), than you should make constructor or even overload operator (). It's more complicated. I suggest not to do it. Unless that has a deeper meaning. If you really want it, it could look like:
struct coordinate
{
int x,y,z;
coordinate(int a, int b, int c) : x(a), y(b), z(c)
{}
void operator()(int a, int b, int c)
{
x = a;
y = b;
z = c;
}
} my_object(10,20,30);
int main()
{
std::cout<<my_object.x<<' '<<my_object.y<<' '<<my_object.z<<'\n';
my_object(30,40,50);
std::cout<<my_object.x<<' '<<my_object.y<<' '<<my_object.z<<'\n';
return 0;
}
You can also do not make constructor and use {} notation.
I'm learning about struct types in C++ and I tried to write a function that would change the values of the members of a struct type variable.
However, it produces an unexpected output and I can't figure out why this is happening.
/Program to test struct types/
#include <iostream>
#include <cstring>
using namespace std;
struct myStruct
{
string a;
string b;
int c;
float d;
};
void assignValues(myStruct myobj)
{
myobj.a = "foobar";
myobj.b = "Foo Bar";
myobj.c = 12;
myobj.d = 15.223;
}
int main()
{
myStruct x;
cout << x.a <<endl;
//x.a = "Hello world";
//x.b = "This is C++";
//x.c = 10;
//x.d = 13.1332;
assignValues(x);
cout << x.a<<endl;
cout << x.b << endl;
cout << x.c << endl;
cout << x.d << endl;
}
If I use the individual assignment statements, (that I have commented out in the code) instead of the assignValues() function, I get the expected output.
PS: The values I expected for the output are as follows:
foobar,
Foo Bar,
12,
15.223
Pass myStruct argument by reference to assignValue function, so it can be modified:
void assignValues(myStruct& myobj)
^^^ pass by reference
{
myobj.a = "foobar";
myobj.b = "Foo Bar";
myobj.c = 12;
myobj.d = 15.223;
}
Although you can pass a pointer to function:
void assignValues(myStruct* myobj) //<---- pointer
{
myobj->a = "foobar";
myobj->b = "Foo Bar";
myobj->c = 12;
myobj->d = 15.223;
}