hey ive got 2 different classes and each class has its own struct with the same type of data... would i be able to create a function that returns the whole sturct from one class to the other?

ive got something basically like this

class A{
  private:
     struct student{
              int age;
              string name;
      }student stu;
  public:
      student returnStu(){ return stu; };
};

class B: public A{
  private:
     struct parent{
              int age;
              string name;
      }student par;
  public:
     void setPar(){ par = A::returnStu(); };


     B operator=(parent &); // just a guess
};

is it possible to return class A's struct in some function to class B? like..

do i need to overload this? and if so how would i write the overload function?

Recommended Answers

All 3 Replies

>>is it possible to return class A's struct in some function to class B?
Yes

>>do i need to overload this? and if so how would i write the overload function?
No, you've to make class B a friend class of A

You have put the struct student as a private data member. Private members are not inherited. Use the protected specifier to inherit student in the class B.

I am still not sure what you exactly want to accomplish. Can you show us by writing a function call in the main(). And this time please post your code in the code tags

[code=cpp] //your code goes here

[/code]

Member Avatar for r.stiltskin

It would be much easier & make more sense to define

struct student{
int age;
string name;
}

outside of any class. Then you can instantiate a student in any class that needs to have one, and use it as an argument or as a return value just like any other variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.