| | |
C++ Object Pointers Problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hi,
Please see the code below.
Now my question is
<< moderator edit: added [code][/code] tags >>
So I assume, object pointers are passed to functions in a different way compared to primitive type pointers. Can anyone pl. comment on this.
Please see the code below.
C++ Syntax (Toggle Plain Text)
void Allocate( char* s ) { s = (char*)malloc( 100 ); } int main( ) { char* s = NULL; Allocate( s ); strcpy( s,"Test");/*I know that this will fail. b'coz I still have a NULL pointer in s. Initially s was pointing to NULL, and from the function Allocate 100 bytes of memory was allocated in some memory location, and address of s was made to point to that location. But inside the main, s is still pointing to NULL. Pl. correct me if I am wrong.*/ }
Now my question is
C++ Syntax (Toggle Plain Text)
void Allocate( Base* s ) { s = new Base; } int main( ) { Base* obj = NULL; Allocate( obj ); obj->some_member = 20; // this works... }
So I assume, object pointers are passed to functions in a different way compared to primitive type pointers. Can anyone pl. comment on this.
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
All passing is by value unless otherwise specified. As you want the changes reflected back to the calling function you will need to pass either a reference to a pointer a char*& or a pointer to a pointer a char** or alternatively return a pointer by value rather than void.
object pointers and primitive type pointers behave exactly the same. Your code is wrong its that simple.
object pointers and primitive type pointers behave exactly the same. Your code is wrong its that simple.
![]() |
Similar Threads
- Purpose of Pointers? (C++)
- C++ pointers problem (C++)
- quicktime VR object loading problem (Graphics and Multimedia)
Other Threads in the C++ Forum
- Previous Thread: substitute of sizeof operator
- Next Thread: how to open/run flash movies from c++
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





