Thread: member as default argument

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248

    member as default argument

    Hi All!!

    My constructor's argument has a default value which is a 'static const' in a .h file. This argument sets a data member in the object to be constructed.

    The argument of one of the member functions now has that data member as default argument.
    But then it complains that that default member is not static. However, I do not want non-const static variables.

    Here's an example of what I mean:
    Code:
    class FilterClass { .. };
    static const FilterClass myFilter1(..);
    
    class A {
     public:
     A(FilterClass const& p_filter = myFilter1)
       : filter(p_filter) { }
      void action(FilterClass const& p_filter = filter)
      {
        ...
      }
     protected:
      FilterClass filter;
    };
    I could not give the static const variables as default arguments in the constructor, but temporaries instead. However, for the member function "action" it becomes problematic:
    the compiler raises an error telling that the argument's default is not static.

    Making the protected data member static resolves the problem, however I do not want non-const static variables.
    Just overloading the function without argument will do, but I am curious to how to resolve the problem.

    Thanks a lot in advance!!

    Mark

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of trying to use a static const global or a member variable to provide the default argument, just use the appropriate constructor of the type:
    Code:
    class FilterClass { .. };
    
    class A {
     public:
     A(FilterClass const& p_filter = FilterClass(..))
       : filter(p_filter) { }
      void action(FilterClass const& p_filter = FilterClass(..))
      {
        ...
      }
     protected:
      FilterClass filter;
    };
    Now, it may be your intention to use the member variable filter if no argument is provided to A::action(). In that case, overload A::action() such that the version with no arguments calls the version that takes a FilterClass by const reference. The version of A::action() that takes an argument would then not provide a default argument.

    EDIT:
    Better yet, define a free function named action in the same namespace as A:
    Code:
    void action(A& a, FilterClass const& filter)
    {
        a.action(filter);
    }
    EDIT:
    Oh wait, I am not thinking correctly: the free function version is not applicable here. I was thinking of having a free function version that only takes an A by reference as the argument, but then you would not be able to provide the member variable as an argument.
    Last edited by laserlight; 03-23-2009 at 08:11 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Yep, indeed these two suggestions I've implemented.

    I definitely wanted to use the static constant variables for clarity of the code, but it was a bad idea after all.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  2. Replies: 6
    Last Post: 07-29-2008, 04:37 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM