I am a little confused with class scope. Lets say that you have a class.

//file.h

struct B
{
   int BB;
   char CC;
};

class A
{

public:
struct B *getStructure();

private:
int number;

};

I dont understand what is the difference between putting struct B inside the class or outside the class. From what I understand if I put the structure inside the class then other classes won't be able to acces that structure. So other classes can't access data types that you create within your class?? Isn't (int number) a data type that other classes can access? Any help clarifying this would be much appreciated. Thanks.

yup...
Struct is also an Data type [ User defined data type]
u can define it where ever u want. the way u use the integer
data type use Struct data type after defining it.
Can u clearly specify u r doubt?
in the above code snippet i dont understand the
Struct B * get...() ?? wat exactly u mean by tat?
u can create an instance of struct , same as int

I was trying to get an element from the Structure by assigning it a pointer to the structure. I was wondering why I couldn't just include the structure within the public part of the class definition. I think this would give me an error when another class tried to instantiate an object of Class A, but I don't understand why. I assume you can't access data types that are specific from a certain class from an outside class. But when you access a private member variable from another class then aren't you doing the same thing.

If you put some struct inside class, then it is defined only inside that class!

The difference is between declaring another datatype, and declaring another instance!

If you declare datatype inside class, it's not available outside of that class!
If you declare datatype outside class, it's available inside and outside of class!

But creating instaces of that datatype is another story...

This might help u , i havent used proper variable names but the code works....
just reply me if u dont understand this code :)

//Header file

#include "stdafx.h"
#ifndef ABC
#define ABC

struct StructA
{
    int x;
    int y;
    char a;
};
class ClassA
{
public:
    ClassA();
    ~ClassA();
private:
    StructA obj;
public:
    StructA Get();
    void Set(StructA obj);
};
class ClassB
{
public:
    struct StructB
    {
        int bb;
        int cc;
        char aa;
    };
    StructB obj;
};

class ClassC
{
public:
    ClassB::StructB obj;
};
#endif

// Implememtation file [Cpp file]

#include "ClassA.h"
ClassA::ClassA()
{
    // Constructor
    obj.x = 0;
    obj.y = 0;
    obj.a = 0;
}
ClassA::~ClassA()
{
    //Destructuctor
}
StructA ClassA::Get()
{
    return obj;
}
void ClassA::Set(StructA _obj)
{
    obj = _obj;
}

// Main Program

// Dummy.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ClassA.h"

int _tmain()
{
    ClassA obj;
    ClassB works;
    StructA dummy;
    dummy.a = 'a';
    dummy.x = 100;
    dummy.y = 200;
    obj.Set(dummy);
    StructA ret_obj;
    ret_obj = obj.Get();
    works.obj.aa = 100;
    works.obj.bb = 200;
    ClassB::StructB CreatedStruct;
    CreatedStruct = works.obj;
	return 0;
}

As u said there i have created a Public struct outside the class , also struct inside the class
both are accessible :)

Don't muddle yourself by a structure (or class) definition inside (or outside) another class and a declaration of a class member. The type S declared inside class X has qualified name X::S (S at X ) and you can use this type (the type - not variable of this type) outside X if S was declared as public (or in derived from X classes if S was declared as protected).

If you declare S outside X then S typename belongs to the scope where it was declared. So it's of no importance where S was declared: it's the same definition of a new type. If it's public name then you can use it (declare new variables of type X::S ).

Feel the difference between a typename and the type variables:

class X {
    struct S { int s; }; // Private X::S definition
public:
    S s; // public variable of private type S
    X::S* newS() { return new S; }
};

int main()
{
    X x;
    X::S* ps = x.newS(); // Illegal: can't use X::S name
    x.s.s = 1; // Legal: assign a value to a public member
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.