Solved

nested classes

Posted on 1998-11-17
7
218 Views
Last Modified: 2012-05-04
how do i access the member function of a class inside another class? the compiler keeps telling me " pointer to structure required on the leftside of -> or ->* "

the declaration of the classes is shown below.

class Point
{
public:
void A();
};

void Point::A()
{
// do nothing
}
class Plane
{
public:
Point tx;
};

void main()
{
Plane *myplane = new Plane;

  myplane->tx->A();//complier reported error on this line.



 
0
Comment
Question by:deuter
  • 3
  • 3
7 Comments
 

Author Comment

by:deuter
ID: 1178111
Edited text of question
0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 1178112
Simply use
myplane->tx.A();
As 'tx' is already an instance of 'Point' (and no pointer to 'Point'), you don't have to dereference it...
0
 

Author Comment

by:deuter
ID: 1178113
thanks, it saves me from redesigning my program but if you dont mind , can you explain is there any difference if i declare my class Point inside the Plane class? is it legal? if yes, is there any difference from the above declaration?

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:jkr
ID: 1178114
It is legal, but gets inconvenient to handle, as the declaration is only valid inside the class (the scope is restricted to it, the declaration is 'local' to the class) - i think it's better to keep your current method...
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178115
>> It is legal, but gets inconvenient to handle
I think he means its inconvenient in this case.  because you will probably want to access the point class from outside fo the place class.  There are plenty of cases where nested classes make a lot of sense and are not inconvenient.  This just isn't one of them.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1178116
nietod - that's exactly what i meant...
deuter - consider the following:
class __DYNLINK CDaoTableDir:   public CObject
{

    class CDaoTableDirEnt
    {
    public:

                            CDaoTableDirEnt ();
                            CDaoTableDirEnt (   CDaoDatabase*   pdaoDB,
                                                CDaoTableDef*   ptd     =   NULL,
                                                CDaoRecordset*  prs     =   NULL
                                            );
        // etc...
    };

public:
    // etc...
};

In this example, the constructor of the nested class is

CDaoTableDir::CDaoTableDirEnt::CDaoTableDirEnt  (   CDaoDatabase*   pdaoDB,
                                                    CDaoTableDef*   ptd,
                                                    CDaoRecordset*  prs
                                                )
{
    m_pdaoTD    =   ptd;
    m_pdaoSet   =   prs;

    Create  (   pdaoDB);
};

and will _only_ be accessible (AND visible) from within instances of 'CDaoTableDir'
0
 

Author Comment

by:deuter
ID: 1178117
jkr, thanks for the info, i 've learnt a lot!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 73
Which IDE to use to begin C++ training? 5 65
c++ getting the first 10 characters of a char* string 11 98
Create a path if not exists 7 89
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 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