Skip to content

Instantly share code, notes, and snippets.

@sakal
Created May 16, 2012 14:37
Show Gist options
  • Save sakal/2710839 to your computer and use it in GitHub Desktop.
Save sakal/2710839 to your computer and use it in GitHub Desktop.
Use object as param for class method in cpp
#include <stdio.h>
#include "class_a.h"
A::A(void) {
}
A::~A(void) {
}
void A::init(B * test) {
printf("result of call init() is '%s'\n", test->test());
}
#include "class_b.h"
#ifndef _CLASS_A_
#define _CLASS_A_ 1
class A
{
public:
A(void);
~A(void);
void init(B* test);
};
#endif
#include "class_b.h"
char * B::test(void) {
return "test";
}
#ifndef _CLASS_B_
#define _CLASS_B_ 1
class B {
public:
char * test(void);
};
#endif
$ g++ ./main.cpp ./class_a.cpp ./class_b.cpp -o main.a
./class_b.cpp: In member function ‘char* B::test()’:
./class_b.cpp:4:9: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
$ ./main.a
result of call init() is 'test'
#include "class_a.h"
#include "class_b.h"
int main() {
A *a_obj = new A();
B *b_obj = new B();
a_obj->init(b_obj);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment