Solved

Nested Class - Separate Class or Struct

Posted on 2004-08-28
10
466 Views
Last Modified: 2013-12-14
Hello

I have working on some code and I need to determine if I should make a small data class a nested class, a structure or a separate class. Currently it is a separate class but I discovered a flaw which requires a fix.

I have a class which inherits from a base class. The base class contains ifstream and ofstream objects which are private. They are used by the derived class to read and write itself. The derived class contains a vector of objects or a data object. I need to be able to read and write the objects when the main class does its read and write.

I would like to avoid the nested class if possible and I am not a fan of structs in C++.  I think I could pass in the ostream and istream objects to the class for reads and writes but this complicates the code and I am not sure it would work.  

Possible Solutions:

1. I could define the class as a struct and create struct objects in the derived class. Would the right and read methods of the struct see the objects of the

2. I could pass in the ifstream and ofstream objects to the data object. Would this work?

3. I believe that a nested class would work fine. Is this correct?

I am looking for solutions that would work and opinions on the best solution of the ones that work.

Thanks,
Andrew
0
Comment
Question by:andrewmchorney
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
Comment Utility
First of all, whithout any code it is hard to give any specific advice, but:

>>2. I could pass in the ifstream and ofstream objects to the data object. Would this work?

This would be IMHO the preferred way, since an implementation like

class Data {

public:

    void write ( ofstream& os);

    void read ( ifstream& is);
};

allows a generalized approach for serializing data or just 'dumping' the ontents to 'cout'.

>>3. I believe that a nested class would work fine. Is this correct?

In general: Yes, but without knowing any specifics, this part is hard to answer.
0
 

Author Comment

by:andrewmchorney
Comment Utility
Here is a better explaination of the class layout

class MyBaseClass
{
      ifstream _ifstream;
      ofstream _ofstream;

      method A()
     {
       // do something
     }
}

class Derived : public MyBaseClass
{
  int x;
  Dataobject obj;
 
  method serialize()
  {
    A.x();
   obj.serialize(_ifstream);
}

class Dataobject
{
  int e;
  int f;
 
  serialize(_ifstream)
  {
     e.A(); // calling method in class Base
     f.A();  // calling method in class Base
  }
}

Can the class Dataobject see methods in the Base C? This is why I think I need to go to either Dataobject being a struct with same code or a nested class.

Thanks,
Andrew
0
 

Author Comment

by:andrewmchorney
Comment Utility
I forgot there is another layer of inheritance:

class SuperTopClass
{
     ifstream _ifstream;
      ofstream _ofstream;

      method A()
     {
       // do something
     }
}
class MyBaseClass : public SupTopClass
{
  //
  // Additional methods that can be implemented by classes such as MyBaseClass
  //
  AdditionalMethodA()
  {
  }
}

class Derived : public MyBaseClass
{
  int x;
  Dataobject obj;
 
  method serialize()
  {
    A.x();
   obj.serialize(_ifstream);
}

class Dataobject
{
  int e;
  int f;
 
  serialize(_ifstream)
  {
     e.A(); // calling method in SuperTopClass
     f.A();  // calling method in SuperTopClass
  }
}

Can the class Dataobject see methods in the Base SuperTopClass? This is why I think I need to change the DataObject class to a struct with same code or a nested class in the Derived Class.

Thanks,
Andrew
class MyBaseClass
{
      ifstream _ifstream;
      ofstream _ofstream;

      method A()
     {
       // do something
     }
}

class Derived : public MyBaseClass
{
  int x;
  Dataobject obj;
 
  method serialize()
  {
    A.x();
   obj.serialize(_ifstream);
}

class Dataobject
{
  int e;
  int f;
 
  serialize(_ifstream)
  {
     e.A(); // calling method in class Base
     f.A();  // calling method in class Base
  }
}

Can the class Dataobject see methods in the Base C? This is why I think I need to go to either Dataobject being a struct with same code or a nested class.

Thanks,
Andrew
class MyBaseClass
{
      ifstream _ifstream;
      ofstream _ofstream;

      method A()
     {
       // do something
     }
}

class Derived : public MyBaseClass
{
  int x;
  Dataobject obj;
 
  method serialize()
  {
    A.x();
   obj.serialize(_ifstream);
}

class Dataobject
{
  int e;
  int f;
 
  serialize(_ifstream)
  {
     e.A(); // calling method in class Base
     f.A();  // calling method in class Base
  }
}

Can the class Dataobject see methods in the Base C? This is why I think I need to go to either Dataobject being a struct with same code or a nested class.

Thanks,
Andrew
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 15

Assisted Solution

by:efn
efn earned 125 total points
Comment Utility
No, as written, a Dataobject cannot see any members of MyBaseClass, because they are all private.  For that matter, a Derived object can't use them either.  To allow a Derived object to call member functions of A, you will have to make those functions public or protected.  For a Dataobject to call member functions of A, those functions must be public, and you must either design a way for a Dataobject to find a MyBaseClass object, or make the functions in A static.  There are various ways to introduce an object to another, and we can advise you about that if it's a problem for you.

Making Dataobject a structure will not help.  The only difference between a class and a structure is the default access, private for a class and public for a structure.  If you made Dataobject a structure, e, f, and Dataobject::serialize would be become public by default, but this will not help Dataobject access MyBaseClass.

Making Dataobject a nested class will not help either.  Although it may seem counterintuitive, a nested class only has its name qualified by the containing class, but it doesn't get any special access privileges.  As far as the containing class is concerned, the nested class is just another class, a stranger.

--efn
0
 

Author Comment

by:andrewmchorney
Comment Utility
I will have to check to see whether the data and method are public. The actual code is not here. I know that derived class can see methods in the methods above it and invoke them so the inheritance must be public.
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 125 total points
Comment Utility
When it comes to thinking this through, you may find it handy to think in terms of IS-A and HAS-A.

class A {/*...*/};
class B {A a; /*...*/};
class C : public B { /*...*/};
class D : private B { /*...*/};
class E : public C { /*...*/};
class F : public D { /*...*/};

Class B "has a" private A called a. B can access anything public in a, but is unaware of anything private or protected in a.

Class C "is a" B. C can access anything public or protected in the B class, but is unaware of anything private in B (i.e. it doesn't know anything about B's a). Similarly class E "is a" C, which means it "is a" B.

Class D "is implemented in terms of " B, because of its private inheritence. Like C, D  can access anything public or protected in the B class, but any class derived from D will be unable to access anything from B's interface. Private inheritence is the twilight zone between IS-A and HAS-A relationships. Class D "isn't really a" B, because class F, which "is a" D "isn't a" B.
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 125 total points
Comment Utility
@rstaveley

Nicely said. I fully agree to all said.

@andrewmchorney

object orientation (OO) means that you should have objects when designing classes. Real objects, not abstract ones. I couldn't see that for classes called 'SuperTopClass' or 'MyBaseClass'. The only OO class (real class) is 'Dataobject' and it isn't derived from anything nor is it a base class, as far i could see from above. I would recommend you to resdesign all above,

Take DataObject as your base class and derive all real objects from that. Have a virtual function serialize() that should be pure virtual in the base class and forget SuperTopClass, MyBaseClass and Derived. Also, omit ifstream and ofstream members - neither private or public - as these io class objects either come from outside (application or document classes, main function, ...) or get created temporarily, e. g. to store a newly created data object. There isn't any need to hold these helpers as a data member.

If you would tell us what you are intending to do we could help you designing your classes.

Regards, Alex





0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now