Member Variable with Limited Scope

Hello all, I've got a beginner question about object orientation in c++...

Let's say I have two interfaces, foo.h and bar.h

1
2
3
4
5
class foo
{
public:
    virtual void DoStuff( ) = 0;
}


1
2
3
4
5
class bar
{
public:
    virtual void DoThings( ) = 0;
}


And let's say I have one class implementing both of these interfaces

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class FooBarImpl : public foo, public bar
{
private:
    int m_dwFooStuff;
    int m_dwBarThings;

public:
    // Foo Methods
    void DoStuff( );

    // Bar Methods
    void DoThings( );

private:
   // Helper Methods
   void CalculateSomeStuff( );
}


Okay, so here's what I want to do. I want the variable m_dwBarThings to ONLY be accessible by methods that were inherited from bar.h. So therefore it is in scope for DoThings( ) but DoStuff( ) does not have access to it, neither do any other non-interface methods like CalculateSomeStuff( )

I know I can just write the code so those methods don't access that member variable lol. I want to prevent future developers from accidentally using that variable somewhere else in the class.

Let's also say that this class can be used/accessed by multiple threads (if that matters to you guys).

Here's my best guess, you guys will suggest that I put m_dwBarThings into bar.h as a protected member variable, then FooBarImpl gets it for free.
You could create two classes. One that inherits foo and another that inherits bar, and let FooBarImpl inherit from both these classes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class FooImpl : public foo
{
private:
    int m_dwFooStuff;

public:
    // Foo Methods
    void DoStuff( );
};

class BarImpl : public bar
{
private:
    int m_dwBarThings;

public:
    // Bar Methods
    void DoThings( );
};

class FooBarImpl : public FooImpl, public BarImpl
{
};
@Peter87 -

Everyone loves layers! (just kiddig)

I think this is the approach I will take.

Thanks.
This seemed a little more obvious to me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class foo
{
public:
    virtual void DoStuff( ) = 0;

protected:
    int m_dwFooStuff;
};

class bar
{
public:
    virtual void DoThings( ) = 0;

protected:
    int m_dwBarThings;
};


@Disch-

That was my initial thought too. Unfortunately I have guidelines I need to follow and my interfaces are all abstract, pure virtual with no evidence as to what goes on in the implementation. Just pure virtual functions and documentation in the interfaces.

Thanks for the input though. Do you think that's a much better solution guidelines aside??

Thanks
No, not really.

Though I don't really see why you'd have the need for the original problem =x. Whether or not you need the variable depends on the implementation, so that variable should be defined in whatever class provides the implementation.
Topic archived. No new replies allowed.