Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++/CLI
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;
};
 

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 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 18-Nov-12 22:43pm
prog786364
Edited 19-Nov-12 5:36am
Nelek47.1K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 CHill60 360
1 Sarvesh Kumar Gupta 238
2 OriginalGriff 193
3 Sergey Alexandrovich Kryukov 163
4 DamithSL 153


Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid