通过使用class作为函数中的参数来访问class的变量 [英] access variables of class by using class as argument in function

查看:68
本文介绍了通过使用class作为函数中的参数来访问class的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我想知道如何将类对象作为函数中的参数传递并使用它
在我创建示例代码的不同地方


Hi All

I want to know how to pass the class object as argument in function and use it
in different places like i created sample code


#pragma once
#include "stdafx.h"
#include "testaaa.h"
#include "test2.h"
using namespace System;

namespace test15
{
  public ref class AA
  {
    public: int funA();
  };
}


#include "Stdafx.h"
#include "A.h"
namespace test15 
{
  int AA::funA()
  {
    testaaa testaaaObj;
    test2bb test2bbObj;// here object is created 
    int k;
    k=testaaaObj.add(test2bbObj,10);// here object can not be passsed in function how to pass object in function and use it 

    return  k;
  }
}


// test15.h
#pragma once
#include "stdafx.h"
#include "test2.h"
#include "A.h"

public ref class testaaa
{
   public :
      int add(test2bb test2bbobj,int b);
};


#include "stdafx.h"
#include "testaaa.h"

int testaaa:: add(test2bb test2bbobj,int b)
{
  //int l,f;
  int k=test2bbobj.xy+b;

  //int k=b+10;
  return k;
}


#pragma once

public ref class test2bb
{
  public:int xy;
};




像这里在主类中的主类中,我创建类testaaa和test2bb类对象,然后通过其对象调用classaaa add函数并将class2bb对象作为参数传递,但在这里我得到问题对象传递不正确的指导,如何将对象作为参数传递并使用它以不同的方式,例如我必须通过对class test2bb对象进行校准来访问class testaaa中的变量xy




like here in main in A class i create class testaaa and test2bb class object then call the function of classaaa add via its object and pass class2bb object as argument but here i get problem object not pass properly kindly guide how to pass object as argument and use it in different ways like i have to access the variable xy in class testaaa by caling class test2bb object

int k=test2bbobj.xy+b;



plz帮助如何在函数中调用对象作为参数并进一步使用它

在此先感谢

[edit]缺少代码块并添加索引[/edit]



plz help how to call object as argument in function and use it further

Thanks in advance

[edit]missing code blocks added and indexation[/edit]

推荐答案

public ref class testaaa
{

public :    int add(const test2bb & test2bbobj,int b);


};



这样,不会复制test2bb对象,但是"add"方法可以与传递给函数的原始对象一起使用.这称为常量引用".您所做的方式可能会导致对象复制,从而对性能产生影响.同样,当您使用const引用甚至不使用const引用时,也不需要实现operator ==(),因为不会复制对象.如果该对象很简单并且不包含堆成员,则编译器生成的隐藏运算符==可以完成此工作,但是如果您的类更复杂,则不会完全复制该对象.

如果您需要修改传递的对象的成员,则必须删除"const"关键字.在某些约定中,如果可以在函数中修改对象,则建议使用指针.

另外,您还需要检查名称空间.也许您需要在函数声明中添加namespace :: test2bb.



That way the test2bb object is not copied but the ''add'' method is working with the original object passed to the function. This is called ''const reference''. The way you did may induce copying of the object and this creates impact on performance. Also when you use const reference or even not const reference you don''t need to implement operator == () because the object is not copied. If the object is simple and does not contain heap members, the compiler generated hidden operator == does the job, but if your class is more complicated it won''t be copied at whole.

If you need to modify members of the passed object than you have to remove the ''const'' keyword. In some conventions if the object can be modified in the function a pointer is preffered.

Also you need to check your namespaces. Maybe you need to add the namespace::test2bb in the function declaration.


这篇关于通过使用class作为函数中的参数来访问class的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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