473,385 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

can a class definition inside another class's definition

Hi,

I want to implement like this:

class A{

int length;

class B{
void operate_length_in_A(){
length++;
}

void use_length_in_A(){
if(length>8){
}
}

B *Bobj;

}

because B will use member in A, so I think I can not put B ahead of
definition of A.

How can I do?

Thanks
Jul 23 '05 #1
1 1762
"Jianli Shen" <ji****@cc.gatech.edu> wrote...
I want to implement like this:

class A{

int length;

class B{
void operate_length_in_A(){
length++;
}

void use_length_in_A(){
if(length>8){
}
}

B *Bobj;

}

because B will use member in A, so I think I can not put B ahead of
definition of A.

How can I do?


Contrary to Java where a nested class is automatically instantiated as
a member of the outer class and also gets all members of the outer class
accessible and associated with the same instance of the outer class as
the nested instance, in C++ a nested class definition is nothing but
a definition of a type.

If you need to access non-static data members of the outer class (like
'length' in your example) from a function that is not a non-static member
of that outer class, you would need an instance to go along with it:

...
class B {
void operate_length_in_A(A& a) {
a.length++;
}
...

Where you put the definition of B inside A shouldn't matter.

V
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Lüpher Cypher | last post by:
Ok, here's what I'm trying to do. I have a class. Inside the class I have a method that reads a text file. In the text file I have a class name and a file name where that class is defined. Now, I...
12
by: Gaurav Veda | last post by:
Hi ! I am a poor mortal who has become terrified of Python. It seems to have thrown all the OO concepts out of the window. Penniless, I ask a basic question : What is the difference between a...
5
by: ganesh.gella | last post by:
Hi All, Wanted to make sure on this, I have some functions for which I am providing the definition inside the class definition. (As these are very small functions like get and set). Do i...
21
by: anddos | last post by:
i am just wondering what is the friend in a class , ive got a book about c++ but it dosent explain deep enough . anyone care to make a example with a few notes on what the friend can benifit from....
2
by: tony | last post by:
Hello! I'm trying to build a class library which has a class called AvestaPlantFunc. In this project building a class libray exist a class called AvestaPlantFunc. In this class is there a...
7
by: for.fun | last post by:
Hi everybody, I have the following problem : B class need A::MyEnum type and A class need B::MyEnum type. In both case, the class type is incomplete so it is obvious that the ::MyEnum can not...
10
by: stonny | last post by:
I read the following sentence from a c++ website, but can not understand why. can anyone help me with it? " An important detail to keep in mind when debugging or implementing a program using a...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
4
by: subramanian100in | last post by:
Consider the program #include <iostream> using namespace std; class Test { public: Test(Test_int c_value)
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.