开发者

c++ access stuff out side the class [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

ok, lets say I have this:

class A:
{
Public:
    int dd;
}

Ok, then I have this class:

class B:
{
Public:
    void sss(){ ff=dd; }
    int ff;
}

Ok, the proble开发者_开发技巧m is that class B doesn't know what dd is. How can I make class B know it?


It's public: and not Public:.

Because dd is part of A, you need an instance of A to access dd.


The two classes aren't related in any way; since B does not have access to any A objects it can't take the value of its members.

If you were to pass an A object to the B constructor, either by value, reference, or pointer, you could access its dd member since you made it public.


You need to have an instance of A, and pass it to the sss method in order to access this data:

void sss(A a) { ff = a.dd; }

If you want to have only one copy of dd, rather than a single copy per instance of A, then you'll have to make dd static:

class A
{
public:
    static int dd;
};

class B
{
public:
    void sss() { ff = A::dd; }
    int ff;
};


Maybe by passing in an A instance?

class B:
{
Public:
    void sss(const A& a){ ff=a.dd; }
    int ff;
}


Your code does not look like C++ but I will try to answer your question.

There would be several possibilities to do what you want. I just pick one which is easy to understand.

Do someting like this:

class B
{
public:
    void sss() { ff = a.dd; }
    int ff;

private:
    A a;
};

However if you do not tell me what you really want to achieve with that class i.e. which responsibilites the class has this will not help you much.


The code that you present, ...

class A:
{
Public:
    int dd;
}

class B:
{
Public:
    void sss(){ ff=dd; }
    int ff;
}

... is not valid C++.

You can find that out easily enough by trying to compile it. Then the compiler will complain about a lot of irrelevant things, but its first message will be related to the extraneous colon after A. When you remove that and compile again, its first message will be related to Public, as opposed to correct public (lowercase). So on.

Syntacically correct code (that will still produce a compilation error, but not about the basic syntax):

class A
{
public:
    int dd;
};

class B
{
public:
    void sss(){ ff=dd; }
    int ff;
};

Imagine that you create five instances of class A, named a1, a2, a3, a4, and a5. Each such instance has a member dd, so you can refer to these five members as a1.dd, a2.dd, a3.dd, a4.dd, and a5.dd.

Now you create a B instance, naming it 'b'. And you call b.sss(). And that member function effectively does b.ff=dd. Or it would, had it been meaningful and accepted by the compiler. But it can't, for which of a1.dd, a2.dd, a3.dd, a4.dd, and a5.dd is being referred to?

So, since this is a very basic concept in C++, you need a good C++ textbook, such as Bjarne's latest, and start from the beginning.

Alternatively, since C++ is a very complicated programming language, it's probably even better to start with some simpler language, like Java, or even simpler, like JavaScript or Python. I recommend Python. It's very different from C++ in the details, but both languages are "conventional" programming languages. Using Python you'll learn about decision constructs and functions and things, including classes versus instances.

Cheers & hth.,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜