Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
#include <iostream>
using namespace std;
void reference(int &ref){
cout << ref << endl;
}
void pointer(int *ref){
cout << *ref << endl;
}
int main(){
int *i = new int[1];
*i = 10;
reference(*i); // fine
reference(i); // why not compiling!!! why not referencing to my pointer??
pointer(i); // fine
}
I want to reference a pointer, as i can see i am allowed to reference value but not pointer, why??
An object of type int* cannot be automatically converted to int&.
I think you are looking for something like:
void reference(int& ref){
cout << ref << endl;
}
void reference(int*& ref){
cout << *ref << endl;
}
Then, you can use both:
int main(){
int *i = new int[1];
*i = 10;
reference(*i);
reference(i);
return 0;
}
This line
reference(i);
is trying to pass in a int * - not an ``int` variable. Hence will not compile.
See the signature of the function
First of all "crash" is a term you can only use after getting through compiler...
void reference(int &ref)
This function is taking reference to integer as its parameter while you are passing pointer to integer through
reference(i)
Change your function's signature to something like:-
void reference(int* &ref)
for this call to work. OR change call to something like:-
int i;
reference(i);
for this function to work.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
To keep it very simple I have a function that passes void (*)(int,int,int) how do I cast to it as a parameter?
example: Function(void (*)(int,int,int) );
I'm expecting to pass parameters here but I got no idea what they are
The declaration of Function expects you to pass it the address of a free function (i.e. one not bound to the instance of a class) that takes three parameters of type int. You didn't specify a return type for Function so for this example I'm going to assume it is void.
#include <iostream>
void Function(void (*)(int, int, int));
void DoSomething(int a, int b, int c)
{
std::cout << "DoSomething called with a=" << a << " b=" << b << " c=" << c;
}
int main()
{
Function(DoSomething);
}
void Function(void (*callback)(int, int, int))
{
std::cout
<< "Function called. Now calling 'callback' at address "
<< callback
<< "\n";
callback(1, 2, 3);
}
In this example main calls Function by passing the address of function DoSomething. The implementation of Function then calls the function it is passed (named callback) and passes 3 arguments 1, 2, and 3.
The result is the following
Function called. Now calling 'callback' at address 00511F55
DoSomething called with a=1 b=2 c=3
You should not need to do any casting to call Function. If you encounter an error or start believing that you should perform a cast to call it you're about to do something terribly wrong.
Is this what you're looking for?
Function(static_cast<void (*)(int,int,int)>(fp))
You might alternatively need const_cast or reinterpret_cast depending on what you're casting...
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
For some reason, I can't seem to get the built-in queue class of C++ to work within a class I create in the way the same built-in queue works in main. I want the queue I use in my class to contain the values of the variables I add to it. But when I use the built-in queue in my class, the queue seems instead to contain something else, maybe addresses of the variables.
What am I doing wrong please?
#include <queue>
#include <iostream>
#include <cstdlib>
using namespace std;
class Myclass {
private:
queue<int> q;
public:
Myclass();
void qPush(int n){ q.push(n); }
int qFront(){ q.front(); }
void qPop(){ q.pop(); }
};
Myclass::Myclass() { // Default Constructor
}
int main () {
int num1 = 0;
int num2 = 1;
queue<int> myQ;
myQ.push(num1);
myQ.push(num2);
cout << myQ.front() << endl;
myQ.pop();
cout << myQ.front() << endl;
cout << "Myclass version: " <<endl;
Myclass b;
b.qPush(num1);
b.qPush(num2);
cout << b.qFront() << endl; // I want this to print out an int. But it looks like it may be printing out an address instead?
b.qPop();
cout << b.qFront() << endl;
return 0;
}
The output I get:
0
1
Myclass version:
537168208
537168212
The problem is that MyClass::qFront() doesn't have a return statement, and because of that it returns a garbage value.
You just need to add the return statement:
int qFront(){ return q.front(); }
To make this code work better you could also add const qualifier to make the method usable with const objects:
int qFront() const { return q.front(); }
Here is an example which demonstrates why it might be necessary:
Myclass a;
a.qPush(42);
const MyClass b = a;
cout << b.qFront(); // This line results in a error if the method isn't marked as const.
The rule here is that you should always mark methods that don't modify object state as const (if you don't have a good reason to do otherwise).
You could also add a second version (overload) of this function which would return a reference to int instead of actual int:
int qFront() const { return q.front(); }
int &qFront() { return q.front(); }
Then the first one would be used for const objects and the second one would be used for mutable ones.
Because it returns a reference to int instead of just a plain int, you could modify the returned value:
Myclass a;
a.qPush(42);
cout << a.qFront();
a.qFront() = 13;
cout << a.qFront();
(Note that compiler wouldn't allow you to write int &qFront() const { return q.front(); }, because it would allow you to modify contents of const objects, which is a bad thing.)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have got a code:
void fun1(int ** i) {
*i = new int(0);
}
void fun2(int *& i) {
i = new int(69);
}
int main()
{
using namespace std;
int a = new int(42);
fun1(&a);
cout << "fun1" << a << endl;
fun2(&a);
cout << "fun2" << a << endl;
}
And I want to pass "a" into functions fun1 and fun2. Can I do it without creating a pointer that will point to a? Can I pass "a" somehow?
One of the functions takes a pointer to a pointer:
void fun1(int ** i)
the other one takes a reference to a pointer:
void fun1(int *& i)
Calling them without having a pointer variable is rather pointless, as those functions main effect (most likely) is to change the adress the passed pointer (passed by reference, either as pointer or reference) is pointing to so that it can be used by the caller.
Lets suppose the following would work
int a;
fun1(&(&a));
The function would change the adress the "temporary" (&a which is a pointer to int) is pointing to, but you would have no means to access it.
1st of all int a = new int(42); is wrong the operator new creates the object on the heap and returns the pointer to the newly created object. you are trying to assign it to an int. at best it should look like int* a = new int; *a = 42; at which time your code is responsible for deleting it.
Now if you have a function like void func(int* pA) {} and have a local variable like int a = 42; and you want to pass it to your function, you would call your function like func(&a);.
2nd both your functions are creating memory leak. you are creating a new pointer that is never deleted.
3rd if you want to change the value of a in your functions, all you need is something like this:
void func1(int* pA)
{
*pA = 42;
}
or you can do something like this:
void func2(int& rA)
{
rA = 42;
}
I think you have not understood the meaning of pointer and references.
To have a reference without pointer, just do foo(int &a) {a = 43} and call the function like this: foo(a)
Operator new returns pointer, so you must write int* a = new int(42);. After you can call the functions as follows:
fun1(&a);
fun2(a);
And also don't forget to delete allocated memory with new:
#include <iostream>
void fun1(int ** i)
{
delete *i;
*i = new int(0);
}
void fun2(int *& i)
{
delete i;
i = new int(69);
}
int main()
{
using namespace std;
int *a = new int(42);
fun1(&a);
cout << "fun1: " << a << endl;
fun2(a);
cout << "fun2: " << a << endl;
delete a;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
if we create a class like this :
class Sales_data
{
std::string isbn() const {return bookNo;}
std::string bookNo;
};
And we make a object total;
Sales_data total;
total.isbn();
The C++ Primer, fifth edition, says (page 258),"when we call a member function, this is initialized with the address of the object on which the function was invoked "
,it like this:
Sales_data::isbn(&total)
and the book also write,we can get the bookNo like :
std::string isbn()const {return this->bookNo;}
I think the implicit parameter "this" just like a pointer,
but i can't see it type,would anybody help me point what wrong i think and what should i do to understand the implicit parameter 'this' and this parameter works for?
#Jason C
my extra question:
this is a pointer,so it behave like a normal pointer,
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int a = 1;
int * b = &a;
cout << "the b is " << b << endl;
cout << "the &a is " << &a << endl;
cout << "the *b is " << *b << endl;
cout << "the &b is" << &b << endl;
return 0;
}
on my computer the output is :
the b is 0110FCEC
the &a is 0110FCEC
the *b is 1
the &b is0110FCE0
then ,What's the use of the type of the pointer.
this is not a parameter, it is a way for an object to refer to itself.
If you use visual studio or any modern IDE you can check that this has the same type as the class of which it is a member of.
There is a good book called "The C++ Object Model" by Stanley B. Lippman which can help understand.
Even if not defined as such in the standard, every implementation I am aware of makes this an implicit parameter to a member function and can be viewed as such.
In C++, you do
object->function () ;
In contrast, in Ada the syntax is
function (object) ;
The object is then an explicit parameter to the member function. The this variable is a product of C++'s member calling syntax. Instead of the programmer having to explicitly declare a parameter identifying the object (as in Ada), C++ does this automatically for you (this).
In most implementations, C++ parameters are bound to offsets to locations on the stack or to registers. This is implemented in the very same way as other parameters (either bound to a stack offset or a register).
this is a pointer to whatever instance of an object the member function is being called on (note that there is no this in static member functions or non-member functions, then).
In your case, it is either a Sales_data * or const Sales_data * depending on the context. Inside isbn(), it is the latter.
This (contrived) example illustrates its value:
class Example {
public:
void function (Example *x);
};
void Example::function (Example *x) {
if (x == this)
cout << "x is this!" << endl;
else
cout << "x is not this." << endl;
}
Now if we do:
Example a;
Example *b = new Example();
a.function(&a); // outputs "x is this!"
b->function(b); // outputs "x is this!"
a.function(b); // outputs "x is not this!"
b->function(&a); // outputs "x is not this!"
Also, since it's a pointer to the "current" instance of the object:
class Example2 {
public:
int k;
void function ();
};
void Example2::function () {
k = 42;
this->k = 42; // does the same thing as above!
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
That may well be silly, but I'm going crazy here ..
Why does pTest doesnt point on the new int after test function in main ? It causes seg fault. And how can I do it.
In my real code I need to pass a pointer and dynamically create the object because pTest is a subclass of a virtual class (reading from file so I don't know in advance)
void test(int* pTest)
{
int *p = new int(2);
pTest = p;
std::cout << "pTest : " << *pTest << std::endl;
return;
}
int main()
{
int *pTest = NULL;
test(pTest);
std::cout << "pTest : " << *pTest << std::endl;
return 0;
}
If you want to mutate value passed as a parameter you have to provide pointer to it. That means if you want to change value of a given int you are passing int* . In your case you want to mutate int* so that you need to pass it as int** instead.
you need to pass pTest by reference: void test(int*& pTest) if you want the pTest value to be altered outside the scope of this function.
void test(int*& pTest)
{
int *p = new int(2);
pTest = p;
std::cout << "pTest : " << *pTest << std::endl;
return;
}