Using THIS pointer with default argument

Pages: 12
Is there a way to set the default value for a class's member function parameter as such:

1
2
3
4
5
6
7
8
9
10
11
12
class String
{
public:
     // Constructors...

     // Following gives a complile error but is there a way 
     // to make it work?
     int rfind( const char *, int position = this.length );

private:
     int length;
}


That way I can have the default argument set without having to pass -1 as the default value and then testing and changing it inside the function.
this is a pointer, not an object.
this->
Yeah I know this isn't an object, but wouldn't:

 
this.length


be an object since length is an object of type int? And this is just pointing to it?

Furthermore, isn't the dot operator used when the variable that is being access isn't a pointer and vice versa for the -> operator?

1
2
3
4
5
String string1;
string1.getString();

String * string2 = new String;
string2->getString();
I don't understand your reasoning.
You know that this is a pointer and you know that . and -> are used for the same, with the difference that the left hand operand for one has to be an object and for the other has to be a pointer. Why would you think that you can do this.something?
Last edited on
Because this.something would be pointing to a member variable something in the class that I'm trying to access. My question ( as I bolded out to avoid these confusions ) was not how does the this pointer works but rather "Is there a way to set the default value for a class's member function parameter"...? Which seems pretty reasonable since you can use this.something within a member function and I don't understand why you can't use it as a parameter's default value.

1
2
3
4
void CLASS::print()
{
     cout << this.something << endl;     // prints the value of something
}


so why can't I access that same value and pass it as a default value in a class function?

1
2
3
void CLASS::print( int value = CLASS::value )
{
}


would that work?
Last edited on
Because this.something would be pointing to a member variable something
this.something this->something would be the member 'something'.

you can use this.something within a member function
No, you can't. this.something is never valid.

EDIT: Have you tried actually compiling that?
Last edited on
Actually I have, I use it all the time to update my private class variables:

1
2
3
4
5
6
7
8
9
String::String( const char * string )
{
     int length;

     for ( int index = 0; string[index] != NULL; index++ )
          length = ( index + 1 );

     this.str_len = length;     // works perfectly fine
}


member variable and member mean the same thing.
Last edited on
this.anything is never valid in C++
member variable and member mean the same thing.
You didn't understand what helios said:
this->something isn't pointing to the member 'something' but it is member 'something'
That's one messed up compiler you got there, romasi.
For something that isn't valid, it works... this references the class's member something. lol

I don't mean to be rude, I really appreciate the help but staying to the question at hand would really make things much simpler.

If I can do cout << this->something << endl;, why can't I pass that same value as a default value to a functions parameter???

NOTE: Complier is Visual Studio 9
Last edited on
1
2
3
4
5
6
7
8
9
10
struct A{
    int a;
    A(){
        this.a=0;
    }
};

int main(){
    return 0;
}

error C2228: left of '.a' must have class/struct/union
type is 'A *const '
did you intend to use '->' instead?


I don't mean to be rude, I really appreciate the help but staying to the question at hand would really make things much simpler.
We would, but the fact that you think this.member is valid is more important.

why can't I pass that same value as a default value to a functions parameter?
According to VC++:
error C2355: 'this' : can only be referenced inside non-static member functions
I can't provide a rationale. You just can't do it.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Test
{
public:
     Test();
     int getNumber();
private:
     int number;
};

Test::Test()
{
     this->number = 5;
}

int Test::getNumber()
{
     return this->number;
}

int main()
{
     Test test();
     cout << test.getNumber() << endl;

     return 0;
}


works perfectly fine, just compiled it right now...
Last edited on
Easy solution:

1
2
int rfind( const char *, int position );
int rfind( const char * s )  { return rfind( s, length ); }
Ok... So what does that prove?

Also, there's a syntax error on line 15, column 1.


EDIT: On a similar line to Disch's post, position could default to an invalid value, like -1.
Last edited on
I already return a -1 as the invalid value, I was trying to find a way to have it default as the actual class member's str_len...

Ok well thanks for the help, I still didn't get an answer to why:

 
int rfind( const char *, int position = this->str_len );


doesn't work when I can use the same statement inside the function... but oh well.
Last edited on
Just one of those "that's just the way it is" things. I can't imagine there'd be a reason why you couldn't other than the language simply doesn't allow it.
As indicated, you can't do that.

The code
int rfind( const char*, int position = this->str_len );
has an out-of-scope variable reference: this simply isn't defined outside the member functions... and the prototype is outside the member functions.

Also, you need to be careful how you bite the hands feeding you. When you post here for advice, and you are given an answer, rather than get angry about our missing the point, you might want to consider that you are missing the point.

Unfortunately, your posts above are so heavily edited that I (nor anyone else) cannot figure out exactly what was written and what wasn't. Your first post was not sufficiently clear; it is entirely reasonable that someone could interpret it as your having difficulty with the syntax as it is posted -- to which the reasonable response is that the syntax is not correct. But to answer the bolded question directly: no, you cannot do that as the language does not permit it. (You cannot use this out of scope.)

However, I can say that the aggressive attitude you have demonstrated in your posts (not just this thread) when learning something new to you is a fair put-off... Who wants to help someone who is rude about the help he receives? For example:
member variable and member mean the same thing.
They most certainly do not. Class members can be data, functions, nested types, and enumerators. You can first assume that any confusion people have about your use of the word "member" is likely because you are using the word incorrectly or without sufficient precision.



As for the -1 thing, an example from the STL might help. The standard string classes (std::basic_string) defines invalid values using the base class constant value npos. Essentially:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<typename _CharT, typename _Traits, typename _Alloc>
  class basic_string
  {
  ...

  public:
    static const size_type npos = static_cast<size_type>(-1);

  ...

  public:
    ...

    basic_string&
    erase(size_type __pos = 0, size_type __n = npos);
  };
(This is from the GCC STL: bits/basic_string.h)

This example shows how to create a constant name that represents the end of a string, and how you can use it in your member function prototypes.

Inside the std::string::erase() function, however, you still have to check to see if the '__n' argument is greater than the string length and adjust it to fit.

Hope this helps.
Ok, first off, you finally answered the question, and the answer being "it's just not possible" which is what I was asking from the beginning. This entire post could have been answered and finished in ONE post and not 20 if you just stated that. If you have other suggestions, they are greatly appreciated, but they should be given as a NOTE in addition to the answer.

Secondly, stating that the question was unclear and then answering it

"But to answer the bolded question directly: no, you cannot do that as the language does not permit it." by Duoas

as if it was perfectly clear to begin with is extremely contradictory...

Thirdly, every post that I made ( and you can check ) in which I was talking about a class's member functions or variables I stated them as such - member variables and member functions - whenever I was referring to a class's variables or functions. However, whether a class's members means all of the above is probably true, it is beyond the point of this question. And as I have to constantly remind readers exactly what the question is...

"Is there a way to set the default value for a class's member function parameter?" and presented an example to show what I mean using the this pointer.

... and above is the point ... not the use of the word member. The person stating the question determines the point of the question, not the reader. To avoid further confrontation with other posters you may want to take that into consideration.

Finally, if someone ask me how a car engine works, I'm not gonna blather on about how the window works, the question is the engine, so stick to the question... if you wanna discuss the window do so after you have answered the question.

I'm not being rude, I'm stating the obvious.

I appreciate the help, and very greatly prefer to avoid confrontation, but ridiculing me for being aggravated when I can't get a simple answer, when a simple answer is quite plausable, makes no sense at all.
Last edited on
romasi actually took the trouble of PMing me his last post, so I'm now going to take the trouble of replying publicly:

I have nothing to do with what what Duoas posted, I took the time to show you that this.member is wrong, despite what your compiler may have been telling you, and I was also the first one to answer your original question:
>>why can't I pass that same value as a default value to a functions parameter?
>According to VC++:
>>error C2355: 'this' : can only be referenced inside non-static member functions
>I can't provide a rationale. You just can't do it.

Some gratitude.

This entire post could have been answered and finished in ONE post and not 20 if you just stated that. If you have other suggestions, they are greatly appreciated, but they should be given as a NOTE in addition to the answer.
To be honest, I though referencing this in the parameter list was possible before attempting it myself, which is why I thought it was just the syntax that was wrong. It's not a hobby of mine to go around wasting time answering questions wrong. If you had posted the error message as the good poster you clearly aren't, it would have been clear what was wrong.

If you just wanted a simple answer, why wasn't the compiler error enough? Is "you can't do that" not simple?

Finally, if someone ask me how a car engine works, I'm not gonna blather on about how the window works, the question is the engine, so stick to the question... if you wanna discuss the window do so after you have answered the question.
On two occasions I have been asked, – "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" In one case a member of the Upper, and in the other a member of the Lower House put this question. I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
There was clearly something wrong with how you asked your question. If you ask "can I start the engine by pulling down the windows?" then of course we're going to try and explain how the windows work. Thinking that you can start a car by pulling down its windows is a much more serious confusion than not knowing how the engine works.
So you didn't know the answer to begin with since you thought it would work yourself. Stating that would be your answer rather then trying to correct this.something vs this->something. I received an error no doubt. I was asking if there was a way to make it work?

"this is a pointer, not an object
this->" by You

isn't a answer or a proper english statement and hence I'm not gonna know what the hell that mean't. I never stated this was an object once in any of my posts. I include
1
2
3
this.something[/b] as a way to reference the something object.

if you had stated... "try [code]String( const char *, int position = this->something ); 
" the first time I would have clearly understood your suggestion to solve my question.

I still don't get how that question was in any way complex, especially since I gave example code showing exactly what I was trying. It really can't get any more clear than that...

All I hear is "wahh wahh wahh!"
Admit that your question was wrong and it couldn't have received the answer you expected.

I looked at your post and the first thing wrong I saw was this.length. Had you posted the error message, I could have told you "no, there's no way to do it, and you're using this wrong."
There was no error message, so I naturally took the first thing I saw.

If I had known I was talking to a strict English parser and not a human being, I would have said "replace all intances of \"this.\" with \"this->\"". I thought anyone with the half of a working brain needed to parse C++ would be able to figure out what I was saying.

The code example was ambiguously wrong and not anyone would have been unequivocally able to tell what was wrong with it. Hell, your compiler thought that this.length was okay.
Pages: 12