访问另一个类中的类的公共成员 [英] Accessing public members of a class in another class

查看:256
本文介绍了访问另一个类中的类的公共成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..



这就是我想要做的事情:

1.传递一个类的对象指针(比如说X) )到另一个类的构造函数(比如Y)。



现在我的问题是:



可以我在类Y的成员函数中访问类X的公共成员?如果可能的话,我想知道如何......因为它给了我一些运行时故障。





提前感谢.. !!





编辑:更新OP的原始问题。 [enhzflep]





这就是我想做的事:



 Class X 
{
public
int ver;
}

class Y
{
y(X * obj)
{
}

void someMethod
{
cout<< obj.ver;
}
}

void main()
{
y obj1 = new y();
y.someMethod;
}





可以这样做吗??

解决方案

< blockquote>是的,你可以。

您当然可以通过 - > 运算符访问对象的公共成员。

我可以我不知道你为什么会遇到运行时故障,但我可能会怀疑你正在调用无效指针的方法(或访问数据)(例如,传递的对象仍然存活 - 在你访问它时还没有被释放?)。





[更新]

  #include   <   iostream  >  
使用 命名空间标准;
class X
{
public
< span class =code-keyword> int ver;
X( int v):ver(v){}
};

class Y
{
X * px;
public
Y(X * pObj):px(pObj){}
void showXVer(){cout<< x version:<< px-> ver<< endl;}
};

int main()
{
X x( 42 < /跨度>);
Y y(& x);
y.showXVer();
}





[/ Update]


是的,只要Y类有一个Class X类型的成员变量。



  #include   <   cstdio  >  

class class1
{
public
bool 已初始化;
bool otherFuncUsed;
class1()
{
initialized = true ;
otherFuncUsed = false ;
}
void sampleFunc()
{
otherFuncUsed = true < /跨度>;
}
};


class class2
{
public
class1 memberClass;
class2()
{
printf( memberClass.otherFuncUsed =%d \\ n \\ n,memberClass.otherFuncUsed);
memberClass.sampleFunc();
printf( memberClass.otherFuncUsed =%d \ n,memberClass.otherFuncUsed) ;
}
};

int main()
{
class2 sampleVar;
}


内容已移至原始问题。


Hi..

This is what I want to do :
1. Pass an object pointer of a class (say X) to the constructor of another class(say Y).

Now my question is :

Can I access the public members of class X in member functions of class Y ? If possible then I want to know how..as it''s giving me some runtime failures.


Thankss in advance..!!


EDIT: Update to original question from the OP. [enhzflep]


This is what i wish to do :

Class X
{
	public:
		int ver;
}

class Y
{
    y(X *obj)
    {
    }
	 
    void someMethod
    {
        cout<< obj.ver;
    }
}

void main()
{
  y obj1=new y();
  y.someMethod;
}



Can this be done.??

解决方案

Yes, you can.
You can access the public members of the object via the -> operator, of course.
I can''t know why are you getting runtime failures, however I might suspect you are calling methods (or accessing data) of an invalid pointer (e.g. is the passed object still alive - has not be released - while you are accessing it?).


[Update]

#include <iostream>
using namespace std;
class X
{
public:
    int ver;
    X(int v):ver(v){}
};

class Y
{
  X * px;
public:
  Y(X * pObj):px(pObj){}
  void showXVer(){ cout << "x version: " << px->ver << endl;}
};

int main()
{
    X x(42);
    Y y(&x);
    y.showXVer();
}



[/Update]


Yes, so long as class Y has a member variable of type Class X.

#include <cstdio>

class class1
{
public:
    bool initialized;
    bool otherFuncUsed;
    class1()
    {
        initialized = true;
        otherFuncUsed = false;
    }
    void sampleFunc()
    {
        otherFuncUsed = true;
    }
};


class class2
{
public:
    class1 memberClass;
    class2()
    {
        printf("memberClass.otherFuncUsed = %d\n", memberClass.otherFuncUsed);
        memberClass.sampleFunc();
        printf("memberClass.otherFuncUsed = %d\n", memberClass.otherFuncUsed);
    }
};

int main()
{
    class2 sampleVar;
}


Content moved to original question.


这篇关于访问另一个类中的类的公共成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆