Thread: pointers as default arguments

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    1

    Question pointers as default arguments

    can anyone telll me how to assign a pointer as a default argument for a c++ function. I tried but clouldn't do anything much useful.Please help.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It's just like any other function defaulting. Here is some code illustrating it.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void f(int* g = 0) {
    	if (g != 0) std::cout << *g << std::endl;
    }
    
    int main() {
    	int a=5;
    	int* pA=&a;
    
    	f();
    	f(pA);
    	f(&a);
    
    	return 0;
    }
    Perhaps you should post code that isn't working for you?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    You don't need the std:: if you #include namespace std;
    Truth is a malleable commodity - Dick Cheney

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Yeah, I know.. I just usually don't do the using namespace std thing .
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM