Reputation: 946
I have a header file and there is a private variable in it
class RoutingTableEntry
{
public:
void SetFlag (RouteFlags flag) { m_flag = flag; }
RouteFlags GetFlag () const { return m_flag; }
private:
RouteFlags m_flag;
}
enum RouteFlags
{
VALID = 0, //!< VALID
INVALID = 1, //!< INVALID
IN_SEARCH = 2, //!< IN_SEARCH
};
I need the value of m_flag
in another class. This .cpp
has the header file in which the the variable is declared.
My doubt is can I use m_flag
from this class, like for e.g.
test.Report (m_flag )
or do I have to use GetFlag ()
Upvotes: 1
Views: 8045
Reputation: 1072
The SetFlag and GetFlag are standard ways of accessing a private member of a class through an instance of that class.
class RoutingTableEntry
{
public:
void SetFlag (RouteFlags flag) { m_flag = flag; }
RouteFlags GetFlag () const { return m_flag; }
private:
RouteFlags m_flag;
}
From the look of the example you've provided:
test.Report(m_flag);
The above implies that test is another class instance (you don't say whether it's an instance of RoutingTableEntry or not, although it actually doesn't make a difference) and are calling a method called Report() whilst trying to supply a member of the RoutingTableEntry class.
Assuming that test is a completely different instantiated class, then passing m_flag to this instance will require you having an instance of RoutingTableEntry to operate on and calling the appropriate method to get the m_flag value. So you end up with the following:
class RoutingTableEntry
{
public:
void SetFlag (RouteFlags flag) { m_flag = flag; }
RouteFlags GetFlag () const { return m_flag; }
private:
RouteFlags m_flag;
};
class MyOtherClass
{
public:
void Report(RouteFlags flags) { ... }
};
RoutingTableEntry entry;
MyOtherClass test;
test.Report(entry.GetFlag());
Since you've not really provided enough information to answer the question, I'll try to guess at what else you may have meant. If you want to use m_flag without needing to call the GetFlag() accessor method, then the Report() method is going to need to be a member of the RoutingTableEntry class. However, what makes this less likely is that if Report was a member of the RoutingTableEntry class, then it wouldn't need to take m_flag as an argument as it could already access it directly.
Upvotes: 0
Reputation: 2804
For your question "Can i use it from this class", the answer is generally yes. When you say use it from this class, it usually means that you are trying to access that variable from one of the functions defined in the class. eg:
class RoutingTableEntry
{
public:
void SetFlag (RouteFlags flag) { m_flag = flag; }
RouteFlags GetFlag () const { return m_flag; }
int anotherFunction() {
//this is valid
test.Report(m_flag) };
}
private:
RouteFlags m_flag;
TestObject test;
}
But your sample code test.Report(m_flag)
looks like you are trying to call a function Report
of a different object test
.
If you are calling this from outside the class ( i.e in a different scope,file ), you would need to call GetFlag
since the variable m_flag
is a private member and can only accessed by the functions of RoutingTableEntry
.
class RoutingTableEntry
{
public:
void SetFlag (RouteFlags flag) { m_flag = flag; }
RouteFlags GetFlag () const { return m_flag; }
private:
RouteFlags m_flag;
}
//code in main
main() {
RouteFlags r1,r2;
TestObject test;
//this is invalid.
test.Report(m_flag)
//this is valid
test.Report(r1.GetFlag());
}
Upvotes: 3