Prag Rao
Prag Rao

Reputation: 1501

Accessing Private data types

#include <iostream>

using namespace std;

class A {
      private :
              typedef struct {
                      int a;
                      int j;
                      }type;
      public : 
             A(){};
            ~A(){};   
            void CreateInstance();               
        };

class B : public  A        
{
      private : 
              int d;
              int n;
      public :
             B(){};
            ~B(){};   
            void CreateInstance1();               

};


void A :: CreateInstance()
{ 
 A::type A;
 A.a = 0x10;     
 cout << " Val = " << A.a << endl;
}

void B :: CreateInstance1()
{ 
 // I want to create a Pointer/instance of structure in this function. Dont want to use Public method in Class A    
 A::type A;
 A.a = 0x10;     
 cout << " Val = " << A.a << endl;
}

int main()
{
 A obj;    
 obj.CreateInstance(); 
 B obj1;
 obj1.CreateInstance1();  
 cin.get();
 return 0;   
}        

I am Looking forward to have some suggestion on this .

  1. How can i create an instance the structure "type" in derived class.

Please let me know how i can use the "data type".

Error : 'typedef struct A :: type A :: type' is Private.

Thanks in Advance.

Upvotes: 0

Views: 118

Answers (5)

theAlias
theAlias

Reputation: 396

My prev answer uses C++11. Here's another way of doing this without auto.

class A {
      private :
              typedef struct {
                      int a;
                      int j;
                      }type;
      public : 
             A(){};
            ~A(){};   
            void CreateInstance();   
            typedef type AType;    **//Added this typedef**
        };

Change your B :: CreateInstance1 to

void B :: CreateInstance1()
{ 
 A::AType A;   **//Now this works**
 A.a = 0x10;     
 cout << " Val = " << A.a << endl;
}

This solution works because private hides the name and not the type as such. So we can can still expose type by wrapping it using public typedef.

Upvotes: 0

theAlias
theAlias

Reputation: 396

If you need it to work so badly, you can try something in these lines

Add a static method in A which creates object instances

static A::type GetTypeInstance();

A::type A :: GetTypeInstance()
{
  A::type lc_Atype;

  return tp;
}

So your B::CreateInstance1 will be

void B :: CreateInstance1()
{ 
 auto A(A::GetTypeInstance());
 A.a = 0x10;     
 cout << " Val = " << A.a << endl;
}

Note that it creates unnecessary temporary objects if your compiler doesnt do RVO

Upvotes: 0

hyde
hyde

Reputation: 62777

Some possibilities:

  • Change type to protected (but you say you can't).

  • Use friend class B; in A (but then you could make the type protected too, I suppose).

  • Ugly hack: re-declare the struct in B, creating identical type. Then use memcpy to copy between variables of the types if needed.

Upvotes: 1

Prag Rao
Prag Rao

Reputation: 1501

#include <iostream>

using namespace std;

class A {
      //woh that worked .. i did mistake here
      friend class B;
      private :
              typedef struct {
                      int a;
                      int j;
                      }type;
      public : 
             A(){};
            ~A(){};   
            void CreateInstance();               
        };

class B : public  A        
{
      private : 
              int d;
              int n;
      public :
             B(){};
            ~B(){};   
            void CreateInstance1();               

};


void A :: CreateInstance()
{ 
 A::type A;
 A.a = 0x10;     
 cout << " Val = " << A.a << endl;
}

void B :: CreateInstance1()
{ 
 // I want to create a Pointer/instance of structure in this function. Dont want to use Public method in Class A    
 A::type A;
 A.a = 0x10;     
 cout << " Val = " << A.a << endl;
}

int main()
{
 A obj;    
 obj.CreateInstance(); 
 B obj1;
 obj1.CreateInstance1();  
 cin.get();
 return 0;   
}        

Upvotes: 0

Michael Simbirsky
Michael Simbirsky

Reputation: 3103

You cannot use anything private from the base class, it is the rule of the language.

You can, however, use anything public or protected. In you case it, probably, will be sufficient to call the base class function CreateInstance

void B :: CreateInstance1()
{ 
    A::CreateInstance();
}

(In general it is better to keep cohesive naming: if applicable, consider declaring the function CreateInstance virtual and then rename CreateInstance1 as CreateInstance to make it an overwrite of A::CreateInstance. It is not related to the question, though).

Upvotes: 1

Related Questions