-4

Hello I need to access data from DataContainer to Derived by Base class. I can't just make this data public because I use this class in place where it shouldn't be accessed.

I could use just friend but then I have to make accessors for Derived classes in the Base class. This will make it inextensible.

#include <vector>

class Data; // It's not important

class DataContainer
{
protected:
        std::vector<Data> dataVector;
        std::vector<Data> dataVector2;
};


class Base
{
protected:
    DataContainer* dataContainer;

public:
    virtual ~Base() {};

    void SetDataContainer(DataContainer* dataContainer)
    {
        this->dataContainer = dataContainer;
    }

    virtual void UseDataFromVector() = 0;
};

class Derived:public Base
{
public:
    virtual ~Derived() {};

    virtual void UseDataFromVector()
    {
        //And here want to use data from DataContainer...
    }

};

My question is how to access this data without making it public or friend.

UPDATE

This answer doesn't help me because friend does not hurt me. I just try to avoid writing lines of code by smart move. I could just write something like this:

    class DataContainer
{
friend class Base
std::vector<Data> dataVector;
};

class Base
{
DataContainer* dataContainer;

std::vector<Data>& GetDataVector() { return dataContainer->dataVector;}
};

But when I add more vectors in DataContainer I'll have to update this class.

Community
  • 1
  • 1
bzfbr
  • 93
  • 1
  • 6
  • Well right now everything in `DataContainer` is private so it is inaccessible to everyone. – NathanOliver Aug 04 '16 at 12:57
  • Yeah. It should be protected. Edited – bzfbr Aug 04 '16 at 13:05
  • 1
    That still does not help. It is still inaccessible. It has to be public or you have to provide accessors. – NathanOliver Aug 04 '16 at 13:07
  • what is the use of `DataContainer` when it has zero public methods? – 463035818_is_not_an_ai Aug 04 '16 at 13:08
  • See [this SO answer](http://stackoverflow.com/questions/424104/can-i-access-private-members-from-outside-the-class-without-using-friends)? Maybe a duplicate? – aichao Aug 04 '16 at 13:09
  • This answer doesn't help me. If this is the only solution it looks like I have to just make friend class. – bzfbr Aug 04 '16 at 13:13
  • Can you update your question as to why that answer does not help you? – aichao Aug 04 '16 at 13:14
  • @tobi303 not in the scope of original question, but such class can be used as base for other classes – mvidelgauz Aug 04 '16 at 13:17
  • Also, there are many answers to that SO post. Have you considered each one? – aichao Aug 04 '16 at 13:21
  • 1
    Looks like your design deserves a review. What is an actual goal? May be it can be reached in a different way? – mvidelgauz Aug 04 '16 at 13:21
  • Thanks @mvidelgauz. I should just inherit from DataContainer. – bzfbr Aug 04 '16 at 13:22
  • @tobi303 I use this class only for handle data. Other classes are working on it. – bzfbr Aug 04 '16 at 13:24
  • You probably wanted to say _"to contain"_ data. You are _handling_ it elsewhere – mvidelgauz Aug 04 '16 at 13:25
  • Hmm.. inheritance still doesn't help. I can now use dataVector member but still I can't get data from DataContainer. – bzfbr Aug 04 '16 at 13:45
  • None of this makes sense. What would be the point of `protected` and `private` if you could just step over them? If you want the members to be publicly accessible, make them `public` or give accessors. If you want them accessible to a descendant, make it `protected`. If you want it accessible to a specific class, make a `friend`. There is no way you can take a class and just ignore `protected` and `private`, otherwise what would be the point of these existing at all? – Taywee Aug 04 '16 at 23:31

2 Answers2

1

The worst hack i've ever seen for this in test code is something like this:

#define private public
#include "the_class_i_want_to_hack.h"

// test case goes here, with instantiation of the hacked class and so on...

I would obviously not recommend it...

Martin G
  • 17,357
  • 9
  • 82
  • 98
0

if you know the structure of the Datacontainer you could abuse pointers and pointer arithmetics

for your DataContainer this would work:

virtual void UseDataFromVector()
{
    std::vector<Data>* pFirstDataContainer = reinterpret_cast<std::vector<Data>*>(dataContainer);
    pFirstDataContainer->emplace_back();
    pFirstDataContainer->size();

    std::vector<Data>* pSecondDataContainer = ++pFirstDataContainer;
    pSecondDataContainer->emplace_back();
    pSecondDataContainer->size();
 }
Tomas Dittmann
  • 424
  • 7
  • 18