#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;
}
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
#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;
}
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.
#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;
}
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). :)