babel92
babel92

Reputation: 777

Odd C++ accessing private member issue

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.

Upvotes: 1

Views: 105

Answers (2)

Ben Voigt
Ben Voigt

Reputation: 283813

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.

Upvotes: 1

Keith
Keith

Reputation: 31

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 ...

Upvotes: 0

Related Questions