I am wondering how can I return an error from a getter method in this situation?
class A
{
private:
int a;
int b;
public:
const int& get(int i)
{
if (i == 1) return a;
else if (i == 2) return b;
// else... what to do here? I get a warning if I don't put anything.
}
};
NOTE: You don't need to return a const int& to your member class, int is enough.
The C++ way for this is Exceptions, but i don't like them and i want suggest some other way based on some situations:
The Getter function must do some operations and then return the result; In this case we could return a std::pair<boo, Type_Of_Result> which is boolean type indicate "It's a successful operation ?":
class A {
private:
int m_value;
public:
A(const int value):
m_value(value) {
}
template<typename T>
using return_t = std::pair<bool, T>;
return_t<int> get() const {
return { true, m_value };
}
};
Okay we got the return value status, But if it fails, how to find why?: In this case we could create a Result class which take care of Error message and return value:
template <typename T>
struct Result {
T return_value;
bool its_okay;
std::string message;
};
class A {
// ...
Result<int> get() const {
return { m_value, true, "All right" };
}
};
I have a simple class which consists of a void pointer and an int (this is some sort of a boost::Variant educational project).
I also have a working copy constructor and a destructor.
But what grinds my gears is, how I would accomplish something like this:
Container cont1("some value"); //simple construction
Container cont2;
cont2.createLink(cont1); //this should initialize members with a reference (or something alike)
std::cout<<cont1; //yields "some value"
cont2.set(20); //setting this container should update the original container too, since I initialized with a reference (or smth alike)
std::cout<<cont1; //yields 20
This is the simplified version of the class:
class Container {
public:
Container(){}
Container(const std::string &val){var.type = STRING; var.data = new std::string(val);}
Container(int val){ /* same for int */}
Container(const Container &val){ /* do a memory copy */}
void set(int val){ /* set the value if type matches, otherwise allocate a new pointer */}
void set(const std::string &val){ /* the same as for int */}
void createLink(const Container &val){ /* somehow assign a reference or whatsoever */}
private:
typedef struct VAR {
int type = 0;
void *data = NULL; }
VAR var;
}
If I set the value of cont2 to a string (i.e. the same data type it holds at the moment), everything is fine, because the set would not allocate a new pointer and rather assign a new value.
But how do I make sure the pointer of cont1 updates if I assign a different value to cont2 and therefore have to allocate a new pointer?
Would I need something like shared_pointer?
Thanks for any insight!
EDIT:
I changed to function name to make it more clear what should happen.
There is a solution that only involves straight OO. You could create an interface for your variant type, and use double indirection to the variant instance to allow linked containers to share the same variant instance.
The reason double indirection is required is because of the way you want the set() method to automatically allocate a new variant instance if the new type doesn't match the original type. If we simply shared a pointer to the variant from both containers, then after set() creates a new variant instance, each container would be referring to different instances again.
To get around that, we can use a pointer to a pointer to a variant in the container instead.
Here is a possible way to define your variant interface, and how it could be subclassed:
typedef std::ostream Out;
struct BadType {};
struct Var {
virtual ~Var () = default;
virtual Out & print (Out &os) { return os << "(BadType)"; }
virtual void set (int) { throw BadType(); }
virtual void set (const std::string &) { throw BadType(); }
};
struct VarInteger : Var {
int data;
VarInteger (int v) : data(v) {}
Out & print (Out &os) { return os << data; }
void set (int v) throw() { data = v; }
};
struct VarString : Var {
std::string data;
VarString (const std::string &v) : data(v) {}
Out & print (Out &os) { return os << data; }
void set (const std::string &v) throw() { data = v; }
};
Here is how you could define your pointer to pointer, and how they could be initialized:
typedef std::shared_ptr<Var> VarPtr;
std::shared_ptr<VarPtr> varptr_;
static VarPtr make_var () { return std::make_shared<Var>(); }
static VarPtr make_var (int v) { return std::make_shared<VarInteger>(v); }
static VarPtr make_var (const std::string &v) {
return std::make_shared<VarString>(v);
}
VarPtr & var () { return *varptr_; }
const VarPtr & var () const { return *varptr_; }
Container () : varptr_(std::make_shared<VarPtr>(make_var())) {}
Container (int v) : varptr_(std::make_shared<VarPtr>(make_var(v))) {}
Container (const std::string &v)
: varptr_(std::make_shared<VarPtr>(make_var(v))) {}
And here is how your set() methods and createLink() method could be implemented.
void set (int v) {
try { var()->set(v); }
catch (BadType) { var() = make_var(v); }
}
void set (const std::string &v) {
try { var()->set(v); }
catch (BadType) { var() = make_var(v); }
}
void createLink (const Container &val) { varptr_ = val.varptr_; }
Demo
How about the following. Of course createLink cannot not take a const reference so I made it to take a non-const pointer.
class Container {
const int STRING = 0x0000001;
const int INT = 0x0000002;
const int LINK = 0x8000000;
public:
...
void set(int val){...}
void set(const std::string &val)
{
if (var.type == LINK)
{
reinterpret_cast<Container*>(var.data)->set(val);
}
else
...
}
void createLink(Container* val)
{
var.data = val;
var.type = LINK;
}
private:
typedef struct VAR {
int type = 0;
void *data = NULL;
};
VAR var;
};
There are a some important points to think about - relative lifetimes of the link and the linked is the most obvious one.
I have the following code:
class STFDataPoint {
public:
virtual ImagePoint get_patch_top_left() const = 0;
virtual ImagePoint get_patch_bottom_right() const = 0;
virtual std::string get_image_filename() const = 0;
virtual ~STFDataPoint() = 0;
};
inline STFDataPoint::~STFDataPoint() {}
class TrainingDataPoint : public STFDataPoint{
private:
int row;
int col;
std::string class_label;
ImagePoint patch_top_left;
ImagePoint patch_bottom_right;
std::string image_filename;
public:
TrainingDataPoint(int row, int col, std::string class_label,
const ImagePoint & top_left,
const ImagePoint & bottom_right,
std::string image_filename);
std::string get_class_label() const;
inline bool operator==(const TrainingDataPoint& other) const{
return other.class_label == this->class_label;
}
inline bool operator!=(const TrainingDataPoint& other) const{
return !(*this == other);
}
virtual ImagePoint get_patch_top_left() const;
virtual ImagePoint get_patch_bottom_right() const;
virtual std::string get_image_filename() const;
};
And I am trying to run the following:
bool do_something(vector<STFDataPoint>& data_point){
return true;
}
int main(int argc, char* argv[]) {
ImagePoint left = ImagePoint(2,3);
ImagePoint right = ImagePoint(2,3);
TrainingDataPoint a = TrainingDataPoint(1,2,"",left, right, "");
vector<TrainingDataPoint> b;
b.push_back(a);
do_something(b);
}
But get the following error:
invalid initialization of reference of type ‘std::vector<STFDataPoint>&’ from expression of type `std::vector<TrainingDataPoint>`
however if I change the signature of do_something() to take in a STFDataPoint (not a vector of them) it runs fine. Can some one please explain why this is and also if there is a work around?
Thanks
Since vector<TrainingDataPoint> is not a subtype of vector<STFDataPoint> you cannot do this. Vectors are not covariant in parameter type.
However you can template do_something to make it work:
template <typename T>
bool do_something(vector<T>& data_point){
//common actions like
ImagePoint leftPatch = data_point[0].get_patch_top_left();
return true;
}
The type vector<TrainingDataPoint> is not the same as vector<STFDataPoint> and there is no conversion between the two. vector<A> is not a base type of vector<B>, even if A is a base of B.
What could work is to have a container of pointers or smart pointers to the base type, and change the function to use that:
bool do_something(vector<std::unique_ptr<STFDataPoint>>& data_point){
return true;
}
std::vector<std::unique_ptr<STFDataPoint>> b;
b.push_back( std::unique_ptr<STFDataPoint>(new TrainingDataPoint(1,2,"",left, right, "") ); // fill with any derived types of STFDataPoint
do_something(b);
Consequently to a previous question about const/non-const with ternary operator, is the following test function ok regarding to the C++11 standard :
template<bool UseConst> class MyClass
{
public:
constexpr bool test()
{
return (UseConst) ? (_constvar) : (_var);
}
protected:
int _var;
static const int _constvar;
}
The whole problem, is that _constvar is const, and _var is non-const. I would have to access these 2 data depending on the template parameter through the same function, and I would like to have a compile-time function when I use const.
Do the test() function satisfy my requirements ?
You could use SFINAE in order to "specialize" your test function. In other words, you could do something like the following:
template<bool true_false>
struct true_type
{
static char value;
};
template<>
struct true_type<false>
{
static char value[2];
};
template<bool UseConst> class MyClass
{
private:
constexpr int pre_test(const char arg) { return _constvar; }
int pre_test(char (&)[2]) const { return _var; }
public:
constexpr int test()
{
return pre_test(true_type<UseConst>::value);
}
protected:
int _var;
static const int _constvar;
};
Now, when you call MyClass::test, if UseConst is false, test will degrade to a run-time function, but when UseConst is true, you will get a compile-time function.
Is there a pattern where I can inherit enum from another enum in C++??
Something like that:
enum eBase
{
one=1, two, three
};
enum eDerived: public eBase
{
four=4, five, six
};
#include <iostream>
#include <ostream>
class Enum
{
public:
enum
{
One = 1,
Two,
Last
};
};
class EnumDeriv : public Enum
{
public:
enum
{
Three = Enum::Last,
Four,
Five
};
};
int main()
{
std::cout << EnumDeriv::One << std::endl;
std::cout << EnumDeriv::Four << std::endl;
return 0;
}
Not possible. There is no inheritance with enums.
You can instead use classes with named const ints.
Example:
class Colors
{
public:
static const int RED = 1;
static const int GREEN = 2;
};
class RGB : public Colors
{
static const int BLUE = 10;
};
class FourColors : public Colors
{
public:
static const int ORANGE = 100;
static const int PURPLE = 101;
};
You can't do that directly, but you could try to use solution from this article.
The main idea is to use the helper template class which holds enum values and has the type cast operator. Considering that the underlying type for enum is int you can use this holder class seamlessly in your code instead of the enum.
Unfortunately it is not possible in C++14. I hope we will have such a language feature in C++17. As you already got few workarounds for your problem I won't provide a solution.
I would like to point out that the wording should be "extension" not "inheritance". The extension allows for more values (as you're jumping from 3 to 6 values in your example) whereas inheritance means putting more constraints to a given base class so the set of possibilities shrinks. Therefore, potential casting would work exactly opposite from inheritance. You can cast derived class to the base class and not vice-verse with class inheritance. But when having extensions you "should" be able to cast the base class to its extension and not vice-verse. I am saying "should" because, as I said such a language feature still doesn't exist.
How about this? Ok an instance is created for every possible value, but besides that its very flexible. Are there any downsides?
.h:
class BaseEnum
{
public:
static const BaseEnum ONE;
static const BaseEnum TWO;
bool operator==(const BaseEnum& other);
protected:
BaseEnum() : i(maxI++) {}
const int i;
static int maxI;
};
class DerivedEnum : public BaseEnum
{
public:
static const DerivedEnum THREE;
};
.cpp:
int BaseEnum::maxI = 0;
bool BaseEnum::operator==(const BaseEnum& other) {
return i == other.i;
}
const BaseEnum BaseEnum::ONE;
const BaseEnum BaseEnum::TWO;
const DerivedEnum DerivedEnum::THREE;
Usage:
BaseEnum e = DerivedEnum::THREE;
if (e == DerivedEnum::THREE) {
std::cerr << "equal" << std::endl;
}
As stated by bayda, enum's don't (and/or shouldn't) have functionality, so I've taken the following approach to your quandary by adapting Mykola Golubyev's response:
typedef struct
{
enum
{
ONE = 1,
TWO,
LAST
};
}BaseEnum;
typedef struct : public BaseEnum
{
enum
{
THREE = BaseEnum::LAST,
FOUR,
FIVE
};
}DerivedEnum;
You can use a project SuperEnum to create extendable enumerations.
/*** my_enum.h ***/
class MyEnum: public SuperEnum<MyEnum>
{
public:
MyEnum() {}
explicit MyEnum(const int &value): SuperEnum(value) {}
static const MyEnum element1;
static const MyEnum element2;
static const MyEnum element3;
};
/*** my_enum.cpp ***/
const MyEnum MyEnum::element1(1);
const MyEnum MyEnum::element2;
const MyEnum MyEnum::element3;
/*** my_enum2.h ***/
class MyEnum2: public MyEnum
{
public:
MyEnum2() {}
explicit MyEnum2(const int &value): MyEnum(value) {}
static const MyEnum2 element4;
static const MyEnum2 element5;
};
/*** my_enum2.cpp ***/
const MyEnum2 MyEnum2::element4;
const MyEnum2 MyEnum2::element5;
/*** main.cpp ***/
std::cout << MyEnum2::element3;
// Output: 3
Kind of hacky but this is what I came up with if dealing with scoped enums:
enum class OriginalType {
FOO, // 0
BAR // 1
END // 2
};
enum class ExtendOriginalType : std::underlying_type_t<OriginalType> {
EXTENDED_FOO = static_cast<std::underlying_type_t<OriginalType>>
(OriginalType::END), // 2
EXTENDED_BAR // 3
};
and then use like:
OriginalType myOriginalType = (OriginalType)ExtendOriginalType::EXTENDED_BAR;
This answer is a variant of Brian R. Bondy answer. Since has been requested in a comment I'm adding it as answer. I'm not pointing about if it really worths though.
#include <iostream>
class Colors
{
public:
static Colors RED;
static Colors GREEN;
operator int(){ return value; }
operator int() const{ return value; }
protected:
Colors(int v) : value{v}{}
private:
int value;
};
Colors Colors::RED{1};
Colors Colors::GREEN{2};
class RGB : public Colors
{
public:
static RGB BLUE;
private:
RGB(int v) : Colors(v){}
};
RGB RGB::BLUE{10};
int main ()
{
std::cout << Colors::RED << " " << RGB::RED << std::endl;
}
Live at Coliru
Well, if you'll define enum with the same name in derived class and start it from last item of correspondent enum in base class, you'll receive almost what you want - inherited enum.
Look at this code:
class Base
{
public:
enum ErrorType
{
GeneralError,
NoMemory,
FileNotFound,
LastItem,
};
};
class Inherited: public Base
{
public:
enum ErrorType
{
SocketError = Base::LastItem,
NotEnoughBandwidth,
};
};
Impossible.
But you can define the enum anonymously in a class, then add additional enum constants in derived classes.
My Solution is similar to some above, except that I wanted to return in my functions like an enum (constructor that takes the STATUS_ENUM value), and compare like an enum (operators that compare the STATUS_ENUM value to the class). I also wanted a clean way of using the base class without having to cast and check things (operator override). Lastly I wanted to make sure that only the type I specify can construct the class (deleted template).
struct StatusReturn
{
/**
* Use this to communicate trigger conditions internally to the caller.
* - Extend this class with a child who adds more static const STATUS_ENUM values as options.
* - When checking the return simply compare with != or == and the class will handle the rest.
* - This is true for a base class and a derived value, since this base class holds the value.
*/
typedef int STATUS_ENUM;
StatusReturn() = delete;
template <typename T>
StatusReturn(T) = delete;
StatusReturn(STATUS_ENUM value): _value(value) {};
// Operator overloads to compare the int to the class
friend bool operator==(const StatusReturn & lhs, const STATUS_ENUM & rhs)
{ return lhs.getValue() == rhs; };
friend bool operator!=(const StatusReturn & lhs, const STATUS_ENUM & rhs)
{ return !(lhs == rhs); };
friend bool operator==(const STATUS_ENUM & lhs, const StatusReturn & rhs)
{ return lhs == rhs.getValue(); };
friend bool operator!=(const STATUS_ENUM & lhs, const StatusReturn & rhs)
{ return !(lhs == rhs); };
// Non-exit triggering return
static const STATUS_ENUM CONTINUE = -1;
// Exit triggering values
static const STATUS_ENUM FAILED = 0;
static const STATUS_ENUM SUCCESS = 1;
static const STATUS_ENUM HALTED = 2;
STATUS_ENUM getValue() const
{ return _value; };
protected:
STATUS_ENUM _value = CONTINUE;
};
Some examples of use:
StatusReturn shouldExit()
{
return successBool ? StatusReturn::SUCCESS : StatusReturn::CONTINUE;
}
Which when called looks like:
auto exitValue = shouldExit();
if (exitValue != StatusReturn::CONTINUE)
{
return exitValue;
}
Then a check of a derived class is as such:
auto exitValue = shouldExit();
if (exitValue != DerivedReturn::DO_STUFF)
{
return exitValue;
}
Here, since DO_STUFF is also a STATUS_ENUM type, the operators just work without any explicit casting.
enum xx {
ONE = 1,
TWO,
xx_Done
};
enum yy {
THREE = xx_Done,
FOUR,
};
typedef int myenum;
static map<myenum,string>& mymap() {
static map<myenum,string> statmap;
statmap[ONE] = "One";
statmap[TWO] = "Two";
statmap[THREE] = "Three";
statmap[FOUR] = "Four";
return statmap;
}
Usage:
std::string s1 = mymap()[ONE];
std::string s4 = mymap()[FOUR];