I'm doing a very small and simple Integer class wrapper in C++, which globaly looks like this:
class Int
{
...
private:
int value;
...
}
I handled almost all the possible assignements, but I don't find out what kind of operator I have to use to get native left assignement.
eg:
Int myInteger(45);
int x = myInteger;
You might want a conversion operator to convert to int:
class Int
{
public:
operator int() const { return value; }
...
};
This allows the following initialization of an int
int x = myInteger;
In C++11, you can decide whether you restrict that conversion to int, or whether you allow further conversions from int to something else. To restrict to int, use an explicit conversion operator:
explicit operator int() const { return value; }
although it is probably not necessary in this case.
Related
I know that a return value is not enough to override a function (and I read different threads about it on stackoverflow), but is there a way to overload the subscript operator of a class, just to return the value (I can't changed to to return by reference-type of function)
It has to look like or at least work like:
What's the best approach to solve this problem (It has to be a operator)?
Update:
The Problem is, that's not allowed to just overload a member or operator just with the return type.
struct A {int a; int b; double c;};
struct B {int a; int b; array<double,2> c;};
class Whatever
{
public:
A operator [](unsigned int ) const; //it's just reading the element
B operator [](unsigned int ) const;
}
A operator [](unsigned int Input){
//...
}
B operator [](unsigned int Input){
//...
}
Assuming you know what kind of type you are going to access, you can return a proxy which converts to either type and, probably, does the access upon conversion. Since you want to access a const object that should be fairly straight forward. Things get a bit more messy when trying to do the same for an updating interface:
class Whatever;
class Proxy {
Whatever const* object;
int index;
public:
Proxy(Whatever const* object, int index)
: object(object)
, index(index) {
}
operator A() const { return this->object->asA(this->index); }
operator B() const { return this->object->asB(this->index); }
};
class Whatever {
// ...
public:
Proxy operator[](int index) { return Proxy(this, index); }
A asA(index) { ... }
B asB(index) { ... }
};
The main constraint is that you can't access members of A and B directly: you need to convert to an A or a B first. If the two types are actually known, you can created a Proxy which forwards the respective member function appropriately. Of course, if there are commonly named member functions or data members you'll need to explicitly convert first.
I need to modify the ordering of my C++ class members. For example:
class B {
public:
int i;
int j;
int k;
...
};
becomes
class B {
public:
int j;
int k;
int i;
...
};
The problem is there are weird codes in my large code bases that depend on relative location of the class members. For example some functions would assume address of member j is smaller than that of member k.
Is there any CASE tool that can help me to identify any code that read the address of a class member?
I am not aware of any tool that solve your problem, but I would define a class which supports all operators for int type and which overloads ampersand operator so that the result of the operator cannot be casted to a pointer. Then I'd use this class instead of int in your class member definitions and look at places where compiler gives errors.
Something like
class IntWrapper {
public:
IntWrapper() { }
IntWrapper(const int x) { } // don't care about implementation as we
operator int() const { return 0; } // want compile-time errors only
IntWrapper& operator ++() { return *this; }
IntWrapper& operator ++(int) { return *this; }
...
void operator &() const { } // make it void so it would cause compiler error
};
And then:
class B {
public:
IntWrapper i;
IntWrapper j;
IntWrapper k;
...
};
This will not help against using boost::addressof function or some dirty reinterpret_cast of a reference, but addressof is probably never used at all in your project, as well as the reinterpret_cast<char&> trick (who would use it for plain integers?).
You should also care about taking an address of the whole object of B class.
I'm reading the article at :
http://www.codeproject.com/Articles/257589/An-Idiots-Guide-to-Cplusplus-Templates-Part-1
and i don't understand some point.
OKAY .How can "Convert" convert itself to any data type?
What does the float() and double() mean at the end of lines below ?? How can converting happen?Could you please explain it with details?
Convert<int>::operator<float> float();
Convert<int>::operator<double> double();
For those of you who wants related part of the article , I'm copy pasting it below and the related part to my question is at the end .
For sure, 'explicit template argument specification' with method template is also possible. Consider another example:
template<class T>
class Convert
{
T data;
public:
Convert(const T& tData = T()) : data(tData)
{ }
template<class C>
bool IsEqualTo( const C& other ) const
{
return data == other;
}
};
Which can be utilized as:
Convert<int> Data;
float Data2 = 1 ;
bool b = Data.IsEqualTo(Data2);
It instantiates Convert::IsEqualTo with float parameter. Explicit specification, as given below, would instantiate it with double:
bool b = Data.IsEqualTo<double>(Data2);
One of the astounding thing, with the help of templates, you can do it by defining conversion operator on top of template!
template<class T>
operator T() const
{
return data;
}
It would make possible to convert the Convert' class template instance into any type, whenever possible. Consider following usage example:
Convert<int> IntData(40);
float FloatData;
double DoubleData;
FloatData = IntData;
DoubleData = IntData;
Which would instantiate following two methods (fully qualified names):
Convert<int>::operator<float> float();
Convert<int>::operator<double> double();
On one hand it provides good flexibility, since without writing extra code, Convert can convert itself (the specific instantiation) to any data-type - whenever conversion is possible at compilation level. If conversion is not possible, like from double to string-type, it would raise an error.
A class type can have user-defined conversions. This allows you to convert an instance of the class into another type. For example, here we have a struct that when converted to an int always has the value 42:
struct A {
operator int() { return 42; }
};
This is a special function, operator int, that is called for conversion to an int. Now if we have an instance of A, a, we can easily convert it to an int:
A a;
int x = a; // x now has the value 42
In the article, they are showing how you can use templates to generate a conversion operator for any type. The function template is defined as:
template<class T>
operator T() const
{
return data;
}
Now, for whenever you try to convert your object into another type, a new version of that function will be generated, replacing T with the type that you're converting to. So if we do float f = a, the following function will generated:
operator float() const
{
return data;
}
This is a function that returns the internal data as a float.
The two lines in the article that are confusing you are just showing you which functions would be generated by the example:
Convert<int>::operator<float> float();
Convert<int>::operator<double> double();
The first is operator float that converts to a float. The second is operator double that converts to a double.
Assuming I have a (trivial) class that wraps normal int type in C++ for random integers, how can I use an instance of this class like an integer when indexing an array or picking a character from a string?
If it's a matter of operator overloading, then which operator?
As a specific example, I have a Random class and I pick characters at random locations in a string like this:
string chars = "whatever";
Random R = Random(0, chars.length());
other_chars += chars.at(R.getValue());
But instead, I'd rather have other_chars += chars.at(R); But how?
You need a user defined conversion operator.
class Random
{
int x_;
public:
operator int() const { return x_; }
};
Have you tried overloading operator int() inside the wrapper Random. I believe it should be something like,
class Random
{
int value;
public:
// ... constructors and operator =
operator int () const { return value; }
};
You need typecast operator overloading: operator int() { return _value; }.
Here is more explanation.
What I'm trying to do is create a new custom data type that behaves like all other primitive types. Specifically, this data type appears like a Fixed Point fraction.
I've created a class to represent this data type, called "class FixedPoint", and in it there are ways to typecast from "FixedPoint" to "int" or "double" or "unsigned int", etc. That is fine.
Now, what if I want to cast from "int" to "FixedPoint"? Originally my solution was to have a constructor:
FixedPoint(int i) { /* some conversion from 'int' to 'FixedPoint' in here */ }
This works...but you cannot put it into a union like so:
union {
FixedPoint p;
};
This will break, because "FixedPoint" does not have an implicit trivial constructor (we just defined a constructor, "FixedPoint(int i)").
To summarize, the whole issue is "we want to cast from some type T to type FixedPoint without explicitly defining a constructor so we can use our type FixedPoint in a union".
What I think the solution is but cannot find any evidence online:
Define an overloaded global typecast operator to cast from "int" to "FixedPoint".
Is there a way to do this without using class constructors? I'd like to be able to use this class in a union. What I've tried (in global scope):
operator (FixedPoint f, int a) { ... } //compiler complains about this is not a method or non-static.
And a little example to show unions don't like constructors (they like POD)
class bob
{
public:
bob(int a) { m_num = a; }
private:
int m_num;
};
void duck()
{
union
{
bob a;
};
}
This error seen in Visual Studio is:
error C2620: member 'duck::<unnamed-tag>::a' of union 'duck::<unnamed-tag>' has user-defined constructor or non-trivial default constructor
Any ideas?
Thanks
I am having a hard time at seeing what you would try to use this for. It seems smelly to have to constantly ensure that sizeof(FixedPoint) == sizeof(int) and, assuming that, there are other hidden gotchas, like endianness. Maybe I should back up a little bit here, unions only "convert" a value from one type to another in that it takes a chunk of memory and references it as a different type. i.e.
union BadConverter
{
int integer;
double fraction;
};
BadConverter.fraction = 100.0/33.0;
BadConverter.integer = ?;
I am pretty sure integer is not going to be 3, it is going to whatever the memory chunk of the double is that the integer bytes share with it.
Unions don't seem to be a very good fit for this sort of thing. I would think just defining a bunch of assignment operators from all the primitive types. i.e.
class FixedPoint
{
FixedPoint& operator=(int value);
FixedPoint& operator=(double value);
..etc..
//Maybe something like this?
template<typename T>
FixedPoint& operator=(const T& value)
{
value = boost::lexical_cast<int>(value);
return *this;
}
}
Custom conversion operators must be a member of the class that is being converted from. A non-trivial constructor is not required.
EDIT: I reworked the example to utilize a union since this is what you were asking about.
EDIT2: See below if you are trying to go the other way (construction) and don't want constructors.
#include <string>
#include <sstream>
using namespace std;
class FixedPoint
{
public:
operator std::string() const
{
stringstream ss;
ss << x_ << ", " << y_;
return ss.str();
}
int x_, y_;
};
union Items
{
FixedPoint point_;
int val_;
};
int main()
{
Items i;
i.point_.x_ = 42;
i.point_.y_ = 84;
string s = i.point_;
}
If you're trying to go the other way -- eg, from an int to FixedPoint in my example -- then the normal way to do this is indeed to use a conversion constructor. Given that you don't want a non-trivial constructor, you have to resort to a conversion function.
FixedPoint make_fixed_point(int x, int y)
{
FixedPoint ret;
ret.x_ = x;
ret.y_ = y;
return ret;
}
union Items
{
FixedPoint point_;
int val_;
};
int main()
{
Items i;
i.point_ = make_fixed_point(111,222);
}
Can't you just add a default constructor that'll allow it to be part of the union.
For example:
class bob
{
public:
bob(int a=0) { m_num = a; }
private:
int m_num;
};
void duck()
{
union
{
bob a;
};
}
By giving the a=0 default, it should be able to be put in a union. I didn't try it myself, though.