Reference argument

So ive been playing around reference. And there is something I dont understand is how to view its value

So i have this code for example

1
2
3
	int i = 70;
	Base b(i);
	std::cout << b.i << std::endl;


1
2
3
4
5
6
7
8
9
class Base
{
public:
	Base(int i);

	~Base();

	int& i;
};



What i get is

17109097

That is the address.. Whats going on? im supposed to return the value
Last edited on
closed account (48T7M4Gy)
line 8 is int& might be it should be int :)
Okay this is really really confusing. I want to understand ref. Why cant I use it on int?
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

class Base
{
public:
	Base(int anI = 0){ i = anI;}
	~Base(){};
	int i;
};

int main()
{
    int a = 70;
	Base b(a);
	
	std::cout << b.i << std::endl;
}
Last edited on
This makes a little sense whats the difference of me using default initializer


1
2
3
Base::Base(int i): wow(i) {
	this->wow = i;
}

.
1
2
3
4
5
6
7
public:
	Base(int i);

	~Base();

	int& wow;
};


They are the same. I initialized the base and why cant I use ref? Im really having a hard time deciding whether to use ref or just normal copy by value
closed account (48T7M4Gy)
Don't split it up. Please put your stuff as a complete deal so that it can be run in the shell. Make it easy on yourself. :)
int& wow;

You can't declare a variable (member) with a reference as a class member. If you insist on using it, consider using a pointer int* wow; instead.
closed account (48T7M4Gy)
whats the difference of me using default initializer

It's not mandatory but can save a lot of messing around aside from being good practice.


closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

class Base
{
public:
	Base(int anI = 0){ p = &anI;}
	~Base(){};
	
	int* p;
};

int main()
{
    int a = 55;
	Base b(a);
	
	std::cout << *(b.p) << std::endl;
}
55
 
Exit code: 0 (normal program termination)
I see. i get it now..

You can't declare a variable (member) with a reference as a class member. If you insist on using it, consider using a pointer int* wow; instead.


Can you tell me why? Pointer is also like a reference. reference points to address so as pointer. So I dont understand the difference and whats the advantage of using one over the other..


Don't split it up. Please put your stuff as a complete deal so that it can be run in the shell. Make it easy on yourself. :)



Sorry. I was really trying to experiment. I have a project that requires me to use c++. And this language is giving me an insane amount of problem.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

class Base
{
public:
	Base(int* anI){ p = anI;}
	~Base(){};
	
	int* p;
};

int main()
{
    int a = 666;
	Base b(&a);
	
	std::cout << *(b.p) << std::endl;
}
666
 
Exit code: 0 (normal program termination)
Hi, Do you know why is it posible to use a pointer and not a reference? pointer is pointing to address same as the reference so why?

Could it be that pointer is an object? Im just guessing here..
closed account (48T7M4Gy)
Hi, Do you know why is it possible to use a pointer and not a reference?


I know what you mean. The way I treat it is that iit is a sequential series of operations based on the primary rule that ptrX = &x and without starting ther you can't pass the pointer directly.

Don't be sorry about experimenting. It's the way to go with pointers etc. ( Be careful though you might get jumped on for not using smart pointers which are another thing altogether but very useful for other reasons. ) The key to experimenting with these is keep reminding yourself of the basics and what each notation means. Note I used * to de-reference the pointer. [ *(b.p) ], which means get the pointer attribute of the object call b, which is p and dereference it thus getting back the value at address (of) a (phew). :)
Topic archived. No new replies allowed.