Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


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

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

C++
#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;
  }
}

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

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

C++
#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;
}

C++
#pragma once

public ref class test2bb
{
  public:int 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

C++
int k=test2bbobj.xy+b;


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]
Posted
Updated 19-Nov-12 5:36am
v2

1 solution

C#
public ref class testaaa
{

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


};


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900