Odd C++ accessing private member issue - c++

I have this piece of code:
class object
{
public:
virtual ~object(){ }
bool equals(const object& J)const
{
return &J == this;
}
int operator==(const object& J)const
{
return equals(J);
}
virtual int getHash()const;
virtual void getType()const;
void* operator new(size_t size)
{
void*mem = malloc(size);
return mem;
}
};
class notcopyable
{
private:
notcopyable(const notcopyable&){}
notcopyable& operator=(const notcopyable&){}
public:
notcopyable(){}
};
class exception :
public object,public notcopyable
{
private:
public:
virtual ~exception();
virtual const char* info();
};
class exception_not_implemented :
public exception
{
public:
exception_not_implemented()
{
}
virtual const char* info()
{
return "exception_not_implemented: ";
}
};
class exception_oob :public exception
{
public:
exception_oob()
{
}
virtual const char* info()
{
return "Index out of boundary";
}
};
There are two functions throw exception_not_implemented:
void object::getType()const
{
throw exception_not_implemented();
}
int object::getHash()const
{
throw exception_not_implemented();
}
And getting this error:
error C2248: 'js::notcopyable::notcopyable' : cannot access private member declared in class 'js::notcopyable'
The output of the compiler says:
This diagnostic occurred in the compiler generated function 'js::exception::exception(const js::exception &)'
If I delete the two throw shown above, it works well. But the same error doesn't happens to exception_oob. I can't figure out why.

You can temporarily add a private copy constructor declaration, which will generate an error at the point where a copy is being made. Then you can fix that code to not make copies.

The error should happen at other place where you call the (private) copy constructor.
For example:
Exception a;
Exception b = a; // error : cannot access private member ...

Related

error: conversion from ‘unique_ptr<GreenStack,default_delete<GreenStack>>’ to non-scalar type ‘unique_ptr<Stack,default_delete<Stack>>’ requested

I am trying to assign a unique_ptr holding a derived class pointer to a unique_ptr holding a base class pointer. However, I am receiving the following error:
error: conversion from ‘unique_ptr<GreenStack,default_delete<GreenStack>>’ to non-scalar type ‘unique_ptr<Stack,default_delete<Stack>>’ requested
Code snippet is below.
class GreenStack;
class Stack {
public:
explicit Stack(double initial_weight) : weight_(initial_weight) {}
static std::unique_ptr<Stack> makeGreenStack(double initial_weight)
{
//std::unique_ptr<Stack> box = std::make_unique<Stack>(initial_weight);
std::unique_ptr<Stack> green_box_01 = std::make_unique<GreenStack>(initial_weight);
return std::move(green_box_01);
}
bool operator<(const Stack& rhs) const { return weight_ < rhs.weight_; }
virtual ~Stack() = default;
protected:
double weight_;
};
class GreenStack:public Stack
{
public:
explicit GreenStack(double initial_weight): Stack(initial_weight){}
~GreenStack() = default;
};
Please guide to resolve this error.
Define Stack::makeGreenStack(double) after defining GreenStack to be derived from Stack.
The compiler will then know that a std::unique_ptr<Stack> can be initialized from a std::unique_ptr<GreenStack>
class GreenStack;
class Stack {
public:
explicit Stack(double initial_weight) : weight_(initial_weight) {}
static std::unique_ptr<Stack> makeGreenStack(double initial_weight);
bool operator<(const Stack& rhs) const { return weight_ < rhs.weight_; }
virtual ~Stack() = default;
protected:
double weight_;
};
class GreenStack:public Stack
{
public:
explicit GreenStack(double initial_weight): Stack(initial_weight){}
~GreenStack() = default;
};
std::unique_ptr<Stack> Stack::makeGreenStack(double initial_weight)
{
std::unique_ptr<Stack> green_box_01 = std::make_unique<GreenStack>(initial_weight);
return green_box_01;
}

Cannot implement base class with virtual getters that are defined in child classes

I have a class that is called Object, this class's header is:
class DLL_SPEC Object {
public:
Object();
virtual ~Object();
virtual std::string getString() const;
virtual void setString(std::string value);
virtual int getInt() const;
virtual void setInt(int value);
virtual double getDouble() const;
virtual void setDouble(double value);
virtual bool isType(FieldType type) const;
};
And my child classes are as follows:
class DLL_SPEC IntObject : public Object {
public:
IntObject() : value(0) {}
IntObject(int v) : value(v) {}
void setInt(int value) override { this->value = value; };
int getInt() const override { return this->value; };
bool isType(FieldType type) const override;
private:
int value;
};
class DLL_SPEC DoubleObject : public Object {
public:
DoubleObject() : value(0.0) {}
DoubleObject(double v) : value(v) {}
void setDouble(double value) override { this->value = value; };
double getDouble() const override { return this->value; };
bool isType(FieldType type) const override;
private:
double value;
};
class DLL_SPEC StringObject : public Object {
public:
StringObject() : value("") {}
StringObject(std::string v) : value(v) {}
void setString(std::string value) override { this->value = value; };
std::string getString() const override { return value; };
bool isType(FieldType type) const override;
private:
std::string value;
};
Now, the problem is, I have an array of Objects and I want to get a string representation of a StringObject.
I call array[0].getString() and even though the object is of type StringObject, the method that gets called is the one is the base class, which I understand.
So, how would I go about implementing that whenever I call getString() on the base class it goes to the child one of the SAME object?
I've tried using this method:
std::string Object::getString() const
{
return dynamic_cast<StringObject*>(this).getString();
}
but then I get an error stating I cannot cast away const or any type qualifier, which is fixed by deleting const modifier (which I MUST leave there as it's according to the task), but then I get another one stating that no suitable constructor exists. So how would I go about implementing this and getting this base class to use the one of the child one?
EDIT: Added a small example that goes into the getString method of Object class and not the StringObject class.
int findPersonId(std::string whereName)
{
Db* db = Db::open("database");
Table* people = db->openTable("table");
auto iteratorTable = table->select();
while (iteratorTable->moveNext())
{
for (size_t i = 0; i < table->getFieldCount(); i++)
{
if (table->getFields()[i]->getName() == "id")
{ //this one beneath goes to the base class and not StringObject
std::string foundRow = iteratorPeople->getRow()[i]->getString();
if (foundRow == whereName)
{
return iteratorTable->getRowId();
}
}
}
}
return 0;
}
Note: The Table* is 2D array that consists of Object** (array that contains StringObject, IntObject, DoubleObject). The method .getRow() return the Object** array that consists of StringObject ...
The way I initiate the objects that go into the array is
Table* table= db->openOrCreateTable("table", 2, userFields); //this creates a 2d array
StringObject* name = new StringObject("Joseph");
IntObject* id = new IntObject(5);
Object** row = combineToRow(id, name);
table->insert(row); //insert an array into 2D array
The method combineToRow is just a simple convertor to Object**.
template<typename A, typename B>
Object** combineToRow(A a, B b) {
return new Object * [2]{ a, b };
}
You have not implemented a getString method for your IntObject, and since you didn't override it you are calling the base method. Once you implement it like this
class IntObject : public Object {
...
virtual std::string getString() const { return std::to_string(value); };
...
};
then you can call it.
int main(){
StringObject* name = new StringObject("Joseph");
IntObject* id = new IntObject(5);
Object** row = combineToRow(id, name);
std::cout << row[0]->getString() << " " << row[1]->getString();
}
5 Joseph
See working version here

Calling method from own exception class catched by its base class

I have two functions a() and b(), which are having own exception classes (consecutively a_exc and b_exc) that inherit from std::logic_error.
void a() { (...) throw a_exc(some_val) }
void b() { (...) throw b_exc(some_val) }
class a_exc : public std::logic_error
{
private:
int foo;
public:
a_exc(int val, const std::string& what_msg="Msg.")
: std::logic_error(what_msg), foo(val) {}
void show() { //show foo }
}
class b_exc : public std::logic_error
{
private:
std::string bar;
public:
a_exc(std::string val, const std::string& what_msg="Msg.")
: std::logic_error(what_msg), bar(val) {}
void show() { //show bar }
}
Let's say I have following part of code:
try {
a();
b();
}
catch (const std::logic_error& e)
{
e.what();
// e.show();
}
catch (const std::logic_error& e) catches both a_exc and b_exc. Of course this block cannot use e.show(), because catched obj is std::logic_error.
And here's my problem. I wonder if there is any chance to call show() method in std::logic_error catch block when catched exception was a_exc or b_exc. I know, calling show() is possible if I create separate catch blocks for a_exc and b_exc, but I want to call this method with using just one catch block. Is it possible?
You can, provided that show() is a const member function:
catch (const std::logic_error& e)
{
e.what();
if(const a_exc* a = dynamic_cast<const a_exc*>(&e))
a->show();
else if(const b_exc* b = dynamic_cast<const b_exc*>(&e))
b->show();
}
See it Live on Coliru. Though, it's usually a bad idea to call other functions that may throw in your catch exception handler.
Some thoughts on design.
Querying the type of exception within the catch block is logically no different to simply providing two catch blocks.
To be clear:
catch(X& x)
{
if (dynamic_cast<Y*>(&x)) {
// it's a Y
}
if (dynamic_cast<Z*>(&z)) {
// it's a Z
}
else {
// it's an X
}
}
is logically the same as:
catch(Y& t)
{
// it's a Y
}
catch(Z& z)
{
// it's a Z
}
catch(X& x)
{
// it's an X
}
Except that the second is clearer, more maintainable and resistant to inadvertent slicing on a subsequent copy.
The first is using "code to find code", which is always a maintenance disaster waiting to happen.
Your question raises more questions of its own:
Are a_exc and b_exc two kinds of the same error? If so, this argues for a polymorphic base class, which you can catch in preference to std::logic_error
Do you really need the show() method? Can you simply build the what string in the constructor, and pass this string to the constructor of std::logic_error? If this is at all possible, it is the route I would recommend. The moment you start adding special interfaces to exceptions, you pollute your entire code base with the necessity of knowing about this interface. If you're writing a library, you've now polluted every application that uses your library.
Assuming you do need show, and a_exc and b_exc really are two kinds of the same error, we can still avoid polymorphism. Perhaps we can shore the 'show' message as a string, and build it in the constructor. Now it's just data. No fuss, no complication.
(complete) example using polymorphic base class (a_exc an b_exc are kinds of the same thing)
#include <stdexcept>
#include <string>
struct showable_logic_error : std::logic_error
{
using std::logic_error::logic_error;
virtual void show() const = 0;
};
class a_exc : public showable_logic_error
{
private:
int foo;
public:
a_exc(int val, const std::string& what_msg="Msg.")
: showable_logic_error(what_msg)
, foo(val)
{}
void show() const override
{
//show foo
}
};
class b_exc : public showable_logic_error
{
private:
std::string bar;
public:
b_exc(std::string val, const std::string& what_msg="Msg.")
: showable_logic_error(what_msg)
, bar(val)
{}
void show() const override
{ //show bar
}
};
void a() { throw a_exc(1); }
void b() { throw b_exc("b"); }
int main()
{
try
{
a();
}
catch(showable_logic_error const& e)
{
e.show();
}
}
complete example in which no polymorphism is required:
#include <stdexcept>
#include <string>
#include <sstream>
struct message_builder
{
template<class T>
static std::string build_what(const std::string& whatstr, T&& info)
{
std::ostringstream ss;
ss << whatstr << " : " << info;
return ss.str();
}
};
class a_exc
: public std::logic_error
, private message_builder
{
public:
a_exc(int val, const std::string& what_msg="Msg.")
: std::logic_error(build_what(what_msg, val))
{}
};
class b_exc
: public std::logic_error
, private message_builder
{
private:
std::string bar;
public:
b_exc(std::string val, const std::string& what_msg="Msg.")
: std::logic_error(build_what(what_msg, std::move(val)))
, bar(val)
{}
};
void a() { throw a_exc(1); }
void b() { throw b_exc("b"); }
int main()
{
try
{
a();
}
catch(std::logic_error const& e)
{
e.show();
}
}
You should consider creating a derived type:
struct show_exc : public std::logic_error
{
virtual void show() = 0;
};
class a_exc : public show_exc
{
int foo_;
public:
virtual void show() override { /*...*/ };
};
and then use a distinguishing catch:
catch (const show_exc& e) {
// ..
}
catch (const std::logic_error& e) {
// ..
}

How to have virtual functions that returns different types based on the class to which it belongs without including dummy implementation in Base?

I need a class hierarchy in which the derived classes will have implementation of a virtual function that differs in the return type. How can i do it. What i have tried is the following code:
using namespace std;
class Base
{
public:
Base()
{
cout<<"Constructor of Base"<<endl;
}
virtual Base& Decode()=0;
virtual operator int(){return -1;}
virtual operator string(){return "WRONG";}
};
class Der1:public Base
{
int i;
public:
Der1(int j=0):Base(),i(j)
{
cout<<"COnstructor of Der1"<<endl;
}
Base& Decode()
{
cout<<"Decode in Der1"<<endl;
return *this;
}
operator int()
{
return i;
}
};
class Der2:public Base
{
string s;
public:
Der2(string temp="sajas"):Base(),s(temp)
{
cout<<"Constructor of Der2"<<endl;
}
Base& Decode()
{
cout<<"Decode in Der2"<<endl;
return *this;
}
operator string()
{
return s;
}
};
int main()
{
Base *p=new Der1();
int val=p->Decode();
}
I was thinking if it could work this way user would just have to equate the object to a valid variable. Is there any way to do it without including all the conversion operators in Base with some dummy implementatin?
I guess there is one problem, if it is a Pure virtual function you cannot create an object of the class base. But on the other hand to solve your problem you can try out using templates, something like below.
#include <iostream>
class Base{
public:
Base(){}
virtual ~Base(){}
virtual void show() const {
std::cout << "Base::show()!" << std::endl;
}
};
class A:public Base{
public:
A(){}
virtual ~A(){}
virtual void show() const{
std::cout << "A::show()!" << std::endl;
}
};
template<typename T>
class Factory{
public:
const T* operator()() const{
return &t;
}
private:
T t;
};
int main(){
const A* pA = Factory<A>()();
pA->show();
Factory<A>()()->show();
return 0;
}

Exception does not get caught

I have a base and a derived exceptions, public inner classes of store:
//base class - ProductException
class ProductException: exception
{
protected:
const int prodNum;
public:
//default+input constructor
ProductException(const int& inputNum=0);
//destructor
~ProductException();
virtual const char* what() const throw();
};
//derived class - AddProdException
class AddProdException: ProductException
{
public:
//default+input constructor
AddProdException(const int& inputNum=0);
//destructor
~AddProdException();
//override base exception's method
virtual const char* what() const throw();
};
this function which throws the derived exception:
void addProduct(const int& num,const string& name) throw(AddProdException);
void Store::addProduct( const int& num,const string& name )
{
//irrelevant code...
throw(AddProdException(num));
}
and a function which calls the function and tries to catch an exception:
try
{
switch(op)
{
case 1:
{
cin>>num>>name;
st.addProduct(num,name);
break;
}
}
}
...
catch(Store::ProductException& e)
{
const char* errStr=e.what();
cout<<errStr;
delete[] errStr;
}
The derived class should get caught, but I keep getting the error "unhandled exception". Any ideas why? Thanks!
The reason is that AddProdException is not a ProductException, because you are using private inheritance:
class AddProdException: ProductException {};
You need to use public inheritance:
class AddProdException: public ProductException {};
The same applies to ProductException and exception, assuming the latter is an std::exception.
Without the public keyword, inheritance is considered private by default. This means AddProdException is-not a ProductException. Use public inheritance like so:
class AddProdException : public ProductException
{
public:
//default+input constructor
AddProdException(const int& inputNum=0);
//destructor
~AddProdException();
//override base exception's method
virtual const char* what() const throw();
};
Also, inherit from std::exception publicly in ProductException as well, otherwise you won't be able to catch std::exceptions either (or even better, from std::runtime_error).