A newbie's questions

M

Michael Bell

Please pardon a newbie's innocent questions.

1) It seems a daft thing, but I can't handle << and >> easily in my
mind unless I have a name for them. I've toyed with :-

<< Corporal

and

(not really knowing anything about military ranks), is there any
standard word I can use in my head and talk to other people about them
and not make a fool of myself?

2) My textbook doesn't explain what the C++ keyword "this" means. I
THINK it means "This next statement uses the scope invoked immediately
above", that is, "there is no need to use :: again, it has already
been defined, just take it as that".

Is this guess correct?



Michael Bell


--
 
C

Chris ( Val )

Please pardon a newbie's innocent questions.

1) It seems a daft thing, but I can't handle << and >> easily in my
mind unless I have a name for them. I've toyed with :-

<< Corporal

Usually known as the 'insertion operator' (think
of it as inserting data into the output stream).

Usually known as the 'extraction operator' (think
of it as extracting data from the input stream).
(not really knowing anything about military ranks), is there any
standard word I can use in my head and talk to other people about them
and not make a fool of myself?

2) My textbook doesn't explain what the C++ keyword "this" means. I
THINK it means "This next statement uses the scope invoked immediately
above", that is, "there is no need to use :: again, it has already
been defined, just take it as that".

Is this guess correct?

No.

The this pointer refers to itself as being the object.

For example:

class Base
{
private:
int Number;
public:

void setNumber( int Number ) {
this->Number = Number;
}

// ...
};

If we did not use the 'this' keyword here, the
assignment would be ambigious because the parameter
Number is the same name as the data member Number.

Other languages have a similar feature, just under a
different name. E.g. 'self' or 'me'.

For the benefit of other newbies, which book are you using?

HTH,
Chris Val
 
C

Chris ( Val )

Usually known as the 'insertion operator' (think
of it as inserting data into the output stream).


Usually known as the 'extraction operator' (think
of it as extracting data from the input stream).




No.

The this pointer refers to itself as being the object.

For example:

class Base
{
private:
int Number;
public:

void setNumber( int Number ) {
this->Number = Number;
}

// ...
};

If we did not use the 'this' keyword here, the
assignment would be ambigious because the parameter
Number is the same name as the data member Number.

[snip]

Just to clarify, without the this keyword being used,
the parameter is just performing self assignment, rather
than setting the appropriate data member.
 
G

Guest

Please pardon a newbie's innocent questions.

1) It seems a daft thing, but I can't handle << and >> easily in my
mind unless I have a name for them. I've toyed with :-

<< Corporal

and


(not really knowing anything about military ranks), is there any
standard word I can use in my head and talk to other people about them
and not make a fool of myself?

They are the left and right shift operators respectively.
2) My textbook doesn't explain what the C++ keyword "this" means. I
THINK it means "This next statement uses the scope invoked immediately
above", that is, "there is no need to use :: again, it has already
been defined, just take it as that".

this is a pointer to the current object in a member function. Consider this:

#include <iostream>

struct Bar
{
int foo;
void baz()
{
int foo = 1;
this->foo = 2;
}
};

int main()
{
Bar b;
b.baz();

std::cout << b.foo;
}

When calling baz() this points to b, so this->foo refers to the member,
while just foo refers to the local variable. In most cases you do not
need to use this, but there are some situations where it is required.
Also some people use it since they think it makes the code easier to read.
 
S

Stuart Redmann

Michael said:
Please pardon a newbie's innocent questions.

1) It seems a daft thing, but I can't handle << and >> easily in my
mind unless I have a name for them. I've toyed with :-

<< Corporal

and




(not really knowing anything about military ranks), is there any
standard word I can use in my head and talk to other people about them
and not make a fool of myself?

I'd suggest to use the vocabulary Bjarne Stroustrup (the creator of C++) uses:
<< called 'inserter' or 'output operator'. >> is refered to as 'extractor'. This
only makes sense when you are talking about streams. When you're talking about
ints, << and >> are called 'left shift' and 'right shift', respectively.

As you can see, there is no single name for '>>' and '<<'. Which name you should
use depends on the context these operators are used in. If this context is
unclear, call them 'operator>>' (spoken: operator greater greater) and
'operator<<' (operator less less).

Regards,
Stuart
 
M

Michael Bell

In message <[email protected]>
Usually known as the 'insertion operator' (think
of it as inserting data into the output stream).

Usually known as the 'extraction operator' (think
of it as extracting data from the input stream).

The this pointer refers to itself as being the object.
For example:
class Base
{
private:
int Number;
public:
void setNumber( int Number ) {
this->Number = Number;
}
// ...
};
If we did not use the 'this' keyword here, the
assignment would be ambigious because the parameter
Number is the same name as the data member Number.
Other languages have a similar feature, just under a
different name. E.g. 'self' or 'me'.
For the benefit of other newbies, which book are you using?
HTH,
Chris Val

I am using "C++ programming in easy steps" by Mike McGrath,
ISBN 1-84078-295-1. It doesn't give much explanation, it seems to
think you can work it out for yourself, which mostly I can. The
explanation of "this" is only 2 lines long, and I can't follow it.

Michael Bell







--
 
S

Stuart Redmann

Michael said:
I am using "C++ programming in easy steps" by Mike McGrath,
ISBN 1-84078-295-1. It doesn't give much explanation, it seems to
think you can work it out for yourself, which mostly I can. The
explanation of "this" is only 2 lines long, and I can't follow it.

Everyone who has not coincidentally a copy of this book at hand would surely
have been grateful to know what these two lines actually are. Admittedly, there
are only very few occasions where one needs the 'this' pointer, most of which
only in the very advanced cases of template programming (which is not a good
point to start with in a beginners book). If you haven't missed the 'this'
pointer at some point, you don't have to know what it is for (although it pays
to do a bit theoretical research on C++ from time to time, there is always some
unrecognized feature of it lurking).

Regards,
Stuart
 
M

Michael Bell

In message <[email protected]>
Everyone who has not coincidentally a copy of this book at hand would surely
have been grateful to know what these two lines actually are.
Admittedly, there
are only very few occasions where one needs the 'this' pointer, most of which
only in the very advanced cases of template programming (which is not a good
point to start with in a beginners book). If you haven't missed the 'this'
pointer at some point, you don't have to know what it is for (although
it pays
to do a bit theoretical research on C++ from time to time, there is
always some
unrecognized feature of it lurking).
Regards,
Stuart

Let me explain a little more about how I got to this position. I did
the Open University Course "MT262: Putting computers to work",
basically a course in C++. I did not thrive on it, though I think that
actually I missed only one or two points, but with computers you've
got to get the WHOLE THING right, otherwise it doesn't work.

If you've read the same words a dozen times and it hasn't sunk in,
reading them a 13th time isn't going to make any difference, so I
bought "C++ programming in easy steps" by Mike McGrath,ISBN
1-84078-295-1, which teaches its material in blocks, unlike the MT262
which makes a point on one page, then develops the next point 10 pages
on, which makes revision very hard.

I think I have made some progress using McGrath, it may be an
irrelevant feature, but it uses THIS a lot. MT262 only mentions it
once, and that as an aside. In another forum, somebody wrote that
"THIS is CENTRAL to C++". Maybe, like many features of maths or
natural languages, you can do the same thing in many different ways
and it's a matter of personal style which way you do it.

McGrath uses THIS a lot, and explains it thus:

"Acessor methods must be defined, as usual. Each object has a special
point named THIS which refers to the object itself. So, object members
can be referred to as THIS -> age, etc. This can be useful in the
accessor method definitions where the argument may often have the
same name as the class member [but isn't it good practice not to do
this?] The THIS pointer can distinguish between the argument and the
class member."

McGrath uses THIS like to put data in like this:-

Dog Fido

Fido.setAge(3);
..
..

void Dog::setAge(int age)
{
THIS -> age = age;
}

and reads it out like this :-

Fido.getAge()
..
..

int Dog::getAge()
{
return age;
}

Am I right that the full "address" or "path" to this nugget of
information is :-

Dog::Fido.age ?

(I have typed THIS wherever it occurs, to make it stand out, but of
course it is normally lower case)

Michael Bell






--
 
G

Guest

"Acessor methods must be defined, as usual. Each object has a special
point named THIS which refers to the object itself. So, object members
can be referred to as THIS -> age, etc. This can be useful in the
accessor method definitions where the argument may often have the
same name as the class member [but isn't it good practice not to do
this?] The THIS pointer can distinguish between the argument and the
class member."

McGrath uses THIS like to put data in like this:-

Dog Fido

Fido.setAge(3);
.
.

void Dog::setAge(int age)
{
THIS -> age = age;
}

and reads it out like this :-

Fido.getAge()
.
.

int Dog::getAge()
{
return age;
}

Am I right that the full "address" or "path" to this nugget of
information is :-

Dog::Fido.age ?

No, that indicates that Fido is a member of Dog (which would either be a
class or a namespace). Unless Fido is in some namespace of class the
full "path" would be ::Fido.age, where the initial :: indicates the
global namespace.
 
J

Jim Langston

Michael Bell said:
Please pardon a newbie's innocent questions.

1) It seems a daft thing, but I can't handle << and >> easily in my
mind unless I have a name for them. I've toyed with :-

<< Corporal

and


(not really knowing anything about military ranks), is there any
standard word I can use in my head and talk to other people about them
and not make a fool of myself?

Well, they really don't have names other than the ones they were originally
used for, shift left operator and shift right operator. Shift left and
Shift right isn't used a whole lot, and there isn't usually a good reason to
need to override them, so "they" decided to use them as they are commonly
seen for input/output to streams. They can be called insertion and
extraction. Usually when I'm coding I don't consciously think of them as
any name other than maybe into my internal dialog as "into" and "outof".
2) My textbook doesn't explain what the C++ keyword "this" means. I
THINK it means "This next statement uses the scope invoked immediately
above", that is, "there is no need to use :: again, it has already
been defined, just take it as that".

Is this guess correct?

this is a pointer to the current instant of the class/structure. In your
sample code you were using it for scope, to differentiate between a
paramater and a class method. this->age meaning the age inside the instance
that this is pointing to. Personally, I just don't pass the same name of a
parameter as a class variable, unless it is in the constructor where I can
use initialization.

I.E.

Dog Fido

Fido.setAge(3);

void Dog::setAge(int age): age( age )
{
}

Fido.getAge()

int Dog::getAge()
{
return age;
}
 
S

Stuart Redmann

Michael said:

[snipped my previous response where I urged the OP to elaborate further]
I think I have made some progress using McGrath, it may be an
irrelevant feature, but it uses THIS a lot. MT262 only mentions it
once, and that as an aside. In another forum, somebody wrote that
"THIS is CENTRAL to C++". Maybe, like many features of maths or
natural languages, you can do the same thing in many different ways
and it's a matter of personal style which way you do it.

McGrath uses THIS a lot, and explains it thus:

"Acessor methods must be defined, as usual. Each object has a special
point named THIS which refers to the object itself. So, object members
can be referred to as THIS -> age, etc. This can be useful in the
accessor method definitions where the argument may often have the
same name as the class member [but isn't it good practice not to do
this?] The THIS pointer can distinguish between the argument and the
class member."

Let's go into some implementation detail. If you have plain functions,
everything is quite simple: every bit of data that should be influenced should
be passed as parameter to the function.

If we are talking about objects and methods, you'll probably have asked yourself
how methods are actually implemented by the compiler. The answer is that they
are implemented just like simple function calls but differ in one respect: As a
method influences the internal state of a particular instance/object (for
example Fido.setAge), the functional implementation needs access to the memory
of the object (in that case the implementation needs to know where the memory
for Fido is located). Thus the functional implementation of methods have a
standard parameter that is the pointer to the object that should be manipulated.
Voila, there you have the 'this' pointer.

If you compile your project with Visual C, and you forget to provide the
implementation of Dog::setAge (int age), the linker will complain about this
missing implementation with the following error message:
error LNK2001: Unresolved external symbols: "public: void __thiscall
Dog::SetAge(int)" (?SetAge@Dog@@QAEXH@Z)
Have a close look at what is written in parenthesis, this is the name of the
internal functional implementation (if you wanted to provide an implementation
of this method with a C compiler, you would have to give this function exactly
this name). As the MS compiler uses name mangling (the name of the function gets
the type of its parameters attached), one can deduct that the internal
functional implementation of Dog::SetAge takes _two_ parameters instead of one:
?SetAge@Dog@@QAEXH@Z
--- - "H" means int parameter.
|--- "Dog" means pointer to Dog object.

McGrath uses THIS like to put data in like this:-

Dog Fido

Fido.setAge(3);
.
.

void Dog::setAge(int age)
{
THIS -> age = age;
}

This is a matter of style. I never use the 'this' pointer for this purpose as I
adopt the Hungarian notation.

Regards,
Stuart
 
J

James Kanze

On 2007-10-08 12:23, Michael Bell wrote:
They are the left and right shift operators respectively.

In C. In C++, they are the insertion and extraction operators
(overloaded to do left and right shift if the lhs is an integral
type, for reasons of C compatibility).
 
M

Michael Bell

In message <[email protected]>
[snipped my previous response where I urged the OP to elaborate further]
I think I have made some progress using McGrath, it may be an
irrelevant feature, but it uses THIS a lot. MT262 only mentions it
once, and that as an aside. In another forum, somebody wrote that
"THIS is CENTRAL to C++". Maybe, like many features of maths or
natural languages, you can do the same thing in many different ways
and it's a matter of personal style which way you do it.

McGrath uses THIS a lot, and explains it thus:

"Acessor methods must be defined, as usual. Each object has a special
point named THIS which refers to the object itself. So, object members
can be referred to as THIS -> age, etc. This can be useful in the
accessor method definitions where the argument may often have the
same name as the class member [but isn't it good practice not to do
this?] The THIS pointer can distinguish between the argument and the
class member."
Let's go into some implementation detail. If you have plain functions,
everything is quite simple: every bit of data that should be
influenced should
be passed as parameter to the function.
If we are talking about objects and methods, you'll probably have
asked yourself
how methods are actually implemented by the compiler. The answer is that they
are implemented just like simple function calls but differ in one
respect: As a
method influences the internal state of a particular instance/object (for
example Fido.setAge), the functional implementation needs access to
the memory
of the object (in that case the implementation needs to know where the memory
for Fido is located). Thus the functional implementation of methods have a
standard parameter that is the pointer to the object that should be
manipulated.
Voila, there you have the 'this' pointer.
If you compile your project with Visual C, and you forget to provide the
implementation of Dog::setAge (int age), the linker will complain about this
missing implementation with the following error message:
error LNK2001: Unresolved external symbols: "public: void __thiscall
Dog::SetAge(int)" (?SetAge@Dog@@QAEXH@Z)
Have a close look at what is written in parenthesis, this is the name of the
internal functional implementation (if you wanted to provide an
implementation
of this method with a C compiler, you would have to give this function
exactly
this name). As the MS compiler uses name mangling (the name of the
function gets
the type of its parameters attached), one can deduct that the internal
functional implementation of Dog::SetAge takes _two_ parameters
instead of one:
?SetAge@Dog@@QAEXH@Z
--- - "H" means int parameter.
|--- "Dog" means pointer to Dog object.
This is a matter of style. I never use the 'this' pointer for this
purpose as I
adopt the Hungarian notation.
Regards,
Stuart

Ah, you've whetted my appetite! What is the Hungarian notation?

Michael Bell


--
 
G

Guest

In message <[email protected]>
[snipped my previous response where I urged the OP to elaborate further]
I think I have made some progress using McGrath, it may be an
irrelevant feature, but it uses THIS a lot. MT262 only mentions it
once, and that as an aside. In another forum, somebody wrote that
"THIS is CENTRAL to C++". Maybe, like many features of maths or
natural languages, you can do the same thing in many different ways
and it's a matter of personal style which way you do it.

McGrath uses THIS a lot, and explains it thus:

"Acessor methods must be defined, as usual. Each object has a special
point named THIS which refers to the object itself. So, object members
can be referred to as THIS -> age, etc. This can be useful in the
accessor method definitions where the argument may often have the
same name as the class member [but isn't it good practice not to do
this?] The THIS pointer can distinguish between the argument and the
class member."
Let's go into some implementation detail. If you have plain functions,
everything is quite simple: every bit of data that should be
influenced should
be passed as parameter to the function.
If we are talking about objects and methods, you'll probably have
asked yourself
how methods are actually implemented by the compiler. The answer is that they
are implemented just like simple function calls but differ in one
respect: As a
method influences the internal state of a particular instance/object (for
example Fido.setAge), the functional implementation needs access to
the memory
of the object (in that case the implementation needs to know where the memory
for Fido is located). Thus the functional implementation of methods have a
standard parameter that is the pointer to the object that should be
manipulated.
Voila, there you have the 'this' pointer.
If you compile your project with Visual C, and you forget to provide the
implementation of Dog::setAge (int age), the linker will complain about this
missing implementation with the following error message:
error LNK2001: Unresolved external symbols: "public: void __thiscall
Dog::SetAge(int)" (?SetAge@Dog@@QAEXH@Z)
Have a close look at what is written in parenthesis, this is the name of the
internal functional implementation (if you wanted to provide an
implementation
of this method with a C compiler, you would have to give this function
exactly
this name). As the MS compiler uses name mangling (the name of the
function gets
the type of its parameters attached), one can deduct that the internal
functional implementation of Dog::SetAge takes _two_ parameters
instead of one:
?SetAge@Dog@@QAEXH@Z
--- - "H" means int parameter.
|--- "Dog" means pointer to Dog object.
This is a matter of style. I never use the 'this' pointer for this
purpose as I
adopt the Hungarian notation.
Regards,
Stuart

Ah, you've whetted my appetite! What is the Hungarian notation?

The original idea behind the Hungarian notation was to prefix all
variables with something that tells you their intent, so variables named
posX and posY describes the position of an object while szX and szY
describes the size, this makes it easier to detect logical erros such as
calling getArea(posX, posY). You can not get the area of a position, but
getArea(szX, szY) makes more sense.

The most common version of Hungarian notation is unfortunately just
redundancy where you prefix variables based on their type, such as
strName (for a string) or pSomething (for a pointer). The value of this
kind of notation is debatable since the typesystem provides the same
functionality.

What Stuart Redmann referred to is probably a lighter version of the
original where you prefix members with an m or similar. So a class would
look something like this:

struct Foo
{
int mBar; // or m_bar, or whatever
Foo(int Bar) : mBar(Bar) {}
};

This way you are never confused about what is and what is not a member.
This scheme can also be extended with prefixes for parameters to
functions and such.
 
G

gw7rib

2) My textbook doesn't explain what the C++ keyword "this" means.

If you're still confused, possibly an example will help. Suppose you
have a class called Thing, which includes two pointer members called
next and previous. Suppose you have a function tieup, which ties up
two Things in the sense that the first one's "next" pointer points to
the second Thing, and the second ones "previous" pointer points to the
first Thing. We can do this as follows:

void tieup(Thing *first, Thing *second) {
first -> next = second;
second -> previous = first;
return;
}

However, this means an external function - tieup - has to be able to
mess about with the internal variables of a Thing. Suppose your boss
does not like this - he thinks Things should be "encapsulated" and
that this sort of thing should be handled by a member function. So you
make one as follows:

void Thing::tieupwith(Thing *nextone) {
next = nextone;
nextone -> previous = this;
return;
}

And, unless I've made an error,

first -> tieupwith(second);

should have the same effect as

tieup(first, second);

Does that help?

Paul.
 
M

Michael Bell

If you're still confused, possibly an example will help. Suppose you
have a class called Thing, which includes two pointer members called
next and previous. Suppose you have a function tieup, which ties up
two Things in the sense that the first one's "next" pointer points to
the second Thing, and the second ones "previous" pointer points to the
first Thing. We can do this as follows:
void tieup(Thing *first, Thing *second) {
first -> next = second;
second -> previous = first;
return;
}
However, this means an external function - tieup - has to be able to
mess about with the internal variables of a Thing. Suppose your boss
does not like this - he thinks Things should be "encapsulated" and
that this sort of thing should be handled by a member function. So you
make one as follows:
void Thing::tieupwith(Thing *nextone) {
next = nextone;
nextone -> previous = this;
return;
}
And, unless I've made an error,
first -> tieupwith(second);
should have the same effect as
tieup(first, second);
Does that help?

Paul

I think I see, but I'm far below the level at which I would need to do
a thing that. But it's all exercise for the grey matter.

Michael



--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top