this pointer related question

Hello, in the code at line 50,
const Stock & Stock::topval(const Stock & s) const
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
what is s? I do not understand what the argument being passed to s is.
I also don't understand why the scope operator is used before topval.

#include <iostream>		
#include <string>		
using namespace std;
class Stock
{
private:
	string company;
	int numshares;
	double price;
public:
	Stock(const string & co, long n = 0, double p = 0.0);
	void show()const;
	double total_val;
	void set_tot() { total_val = numshares * price; }
	const Stock & Stock::topval(const Stock & s) const;
};
const int numarr = 2;
int main()
{
	Stock stocks[numarr] = { Stock("Apple", 10, 50.00),
		Stock("Micro", 999, 3.00) };
	cout << "Stock holdings:\n";
	int st;
	for (st = 0; st < numarr; st++)
		stocks[st].show();
	const Stock * top = &stocks[0];
	for (st = 1; st < numarr; st++)
		top = &(*top).topval(stocks[st]);
	cout << "\nMost valuable holding:\n";
	top->show();
	return 0;
}
Stock::Stock(const string & co, long n, double p)
{
	company = co;
	numshares = n;
	price = p;
	set_tot();
}
void Stock::show() const
{
	cout << "Company: " << company << endl;
	cout << "Numshares: " << numshares << endl;
	cout << "Price: " << price << endl;
	cout << "Total price: " << total_val << endl << endl;
}
const Stock & Stock::topval(const Stock & s) const
{
	if (s.total_val > total_val)
		return s;
	else
		return *this;
} 
Last edited on
lines 4-19, which hold the Stock class declaration, would preferably be found in their own header file, e.g. Stock.h , behind an include guard.

Your post has both the declaration and the implementation in the same file, which is still valid.
Line 18 could be written without the scope as const Stock& topval(const Stock& s) const; , since topval method is already inside the brackets of class Stock { ... } . The implementation of the Stock methods below that all need to start with Stock:: to show that they're implementing something from the Stock class (otherwise it would be a global method).

topval looks like it's simply comparing the current object's Stock to another Stock object (named s) topval takes a constant reference to another Stock to show that it will not attempt to change this argument. More specifically, it's comparing values between this stock and another, returning the bigger one.
Thanks very much for your reply. This is an adaption of another program which did include header files etc. I put it like this to try to make it easier for me to see what is going on. Your explanation about topval comparing values helped me a lot to understand how the this pointer was working. However, when I tried getting rid of the scope resolution operator and substituting in:
const Stock& topval(const Stock& s) const;
in the class and the definition, I got this compiler error message:
modifiers not allowed on nonmember functions.
Registered users can post here. Sign in or register to post.