Reputation: 9
just guess we have class like
class A{
public:
struct B{
...
};
typedef B C;
};
then, I'd like to use the C type in other class, that is other cpp.
how to use it?
Thanks
Upvotes: 0
Views: 74
Reputation: 2365
If you declare a class, then in the class declaration you are in the namespace of the class. Let's have a look at a simple class:
class Sample{
public:
void doSomething();
};
And let's say we also have a namespace:
namespace sampleNamespace{
void doAnotherThing();
}
If you want let's say a pointer for both of those functions you would say:
auto* ptrFunction = Sample::doSomething;
auto* ptrAnotherFunction = sampleNamespace::doAnotherThing;
So the syntax itself is the same. Now let's take your example into account:
class Sample{
public:
struct A{};
typedef B A;
};
and let's convert it to the namespace conventions:
namespace Sample{
struct A{};
typedef B A;
}
Now if you want to use struct A you can do both (but members must be publicly visible):
//first option
Sample::A variableName;
//second option
Sample::B variableName;
Although you cannot of course use using namespace to achieve this :) So in general in cases like this you can think of classes like of namespaces.
Upvotes: 3
Reputation: 991
You can simply just include the .h
i.e header
file and use A::C. For e.g
class Demo {
public:
struct A {
int data;
};
typedef A B;
};
#include <iostream>
#include "Header.h" // This is important
int main() {
Demo::B *v = new Demo::B;
v->data = 5;
// This will print "The data is: 5"
cout << "The data is: " << v->data << endl;
delete(v);
return 0;
}
Upvotes: 0