Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,717 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,264 people online right now. Registration is fast and FREE... Join Now!




returning a struct from a function

 
Reply to this topicStart new topic

returning a struct from a function, returning a struct from one class to another.

usmsci
5 Feb, 2007 - 08:38 PM
Post #1

New D.I.C Head
*

Joined: 5 Feb, 2007
Posts: 1



Thanked: 1 times
My Contributions
hi all. i guess i need to brush up on my c++ classes cos this one has me stumped.

my situation is is that i have 3 classes. for simplicity lets call them Class A, B & C.

Class A instaniates Class B which by inheritance instantiates Class C. Therefore Class A has access to all of the public member functions that Class B does. thats not even my problem just laying the groundwork. therefore:
CODE

//in class A.h
#include <B.h>
.....
B *reader;
......
struct myStructA
{
string name;
int id;
};
myStructA *x;
------------------------------
//in class A.cpp
reader = new B();


CODE

//in classB.h
Class B :  public Class C
{

};


In Class C i have a struct that i have defined as such:

CODE

struct myStructC
{
  string name;
  int id;
};
myStructC *x;


eventually i will find out how many of this type i will need and like so:

x = new myStruct[some_number];

then i will proceed to fill up this in memory. easy enough.

Now what i want to do is write a function in this class to return this struct to Class A so Class A can use the contents of the data. this will require Class A to call the public member getData in Class C and this is where the heart of my question/problem lies.

so here is my Class C function to return the struct:
CODE

ClassC::myStructC ClassC::getData()
{ return *x; }


now is this correct or am i missing something here?

if this is correct what do i need to in Class A to make sure it calls this function correctly because right now all i get are compiler errors. i would print them here but i have tried so many different things. so far what i have done in Class A is define the exact same struct so when class A calls the getData function it can receive a copy of the struct that is returned from Class C: so one of the things i have tried so far is:

myStructA = reader->getData(); -- where reader up above is defined. this would be the ideal way it would seem to solve this problem but sadly this isnt compiling.

can anyone help me out here with some code to make this happen? just FYI this isnt homework but its a roadblock to something that i working on, on my own. if you could help i would be eternally grateful smile.gif thanks all in advance!
User is offlineProfile CardPM
+Quote Post

cdk
RE: Returning A Struct From A Function
6 Feb, 2007 - 03:19 PM
Post #2

New D.I.C Head
*

Joined: 17 Jan, 2007
Posts: 15


My Contributions
QUOTE(usmsci @ 5 Feb, 2007 - 09:38 PM) *

...
if this is correct what do i need to in Class A to make sure it calls this function correctly because right now all i get are compiler errors. i would print them here but i have tried so many different things.


I had a hard time telling what you wanted because there's a lot to unravel in this question and because you have things defined twice (myStructA and myStructC both define the same structure).

I think your situation is that Class C uses Class B, which itself inherits Class A.

The 'uses' part of your requirement is incorrect and the inheritance isn't right either -- or you didn't provide the definition for Class C in your text.

I'll assume you want multiple instances of all Classes -- i.e., you don't want static instances.

In OOP terminology, the 'uses' and 'has-a' are the same thing. Any good C++ OOP book will use either one of those terms -- so you can read more there.

An outline for the standard way of implementing a 'uses' or 'has-a' relationship using the symbol names in your example is like this:

CODE

// classC.h:
struct myStructC
{
  string name;
  int id;
};
------------
// classB.h:
#include "classC.h"
class B :  public myStructC
{
};
--------------
// classA.h:
#include "classB.h"
class A {
  public:
  // Class A uses Class B:
  B b;
};
--------------
// main.cpp:
#include "classA.h"
main() {
  A a;
  a.b.name = "curtis";
  a.b.id = "cdk";

  A second;
  second.b.name = "test";
  second.b.id = "usmsci";
}


Warning: the above is untested and off the top of my head. Some syntax adjustments might need to be made.

Some things I noticed while looking over your code:

The Class keyword is 'class' (lowercase).

The inheritance syntax doesn't reuse the word 'class'.

The 'has-a' methodology puts the declaration of the object being used inside of the declaration of the class that uses it.

Hope that helps.

cdk

This post has been edited by cdk: 6 Feb, 2007 - 03:24 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 04:46AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month