Created
May 16, 2012 14:37
-
-
Save sakal/2710839 to your computer and use it in GitHub Desktop.
Use object as param for class method in cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "class_b.h" | |
#ifndef _CLASS_A_ | |
#define _CLASS_A_ 1 | |
class A | |
{ | |
public: | |
A(void); | |
~A(void); | |
void init(B* test); | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "class_b.h" | |
char * B::test(void) { | |
return "test"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _CLASS_B_ | |
#define _CLASS_B_ 1 | |
class B { | |
public: | |
char * test(void); | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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