R
Roger Leigh
Hello again,
I have a bit of a strange problem here. This code works OK:
class foo
{
public:
foo(int **ref):
m_ref(ref)
{}
private:
int **m_ref;
};
int main()
{
int i = 1;
int *iptr = &i;
foo bar(&iptr);
return 0;
}
but this code does not:
class foo
{
public:
foo(int& *ref):
m_ref(ref)
{}
private:
int& *m_ref;
};
int main()
{
int i = 1;
foo bar(&i);
return 0;
}
I get this error when I compile it:
$ g++ -o test test.cc
test.cc:4: error: cannot declare pointers to references
test.cc:8: error: cannot declare pointers to references
This is just a testcase. From another compiler error, it looks like
the compiler (GCC) thinks that the prototype for foo::foo() is
actually foo::foo(int*), not foo::foo(int&*).
The reason I want this behaviour is as follows: I have a database
"transaction object" representing a transaction. It is allocated on
the heap with new, and passed to the constructors of several classes
which are instantiated by the parent class (and so on, recursively).
All the classes are part of the same database transaction, and so need
access to the same transaction object.
Now, I can't pass the object by value as a reference: it's perfectly
valid for it to be NULL, which causes transactions to be dynamically
created as required, rather than using the user-supplied one. It's
also valid for a child to delete the transaction object and/or create
a new one, in which case I want the pointer in the parent and other
children to be updated. This won't happen if I just pass a plain
pointer, since the pointer is passed by value. I could implement this
as a pointer to a pointer (see top example), but it seemed cleaner as
a reference to a pointer, since all the children could treat it as a
plain pointer, without having to dereference it twice.
Could anyone point out what I am misunderstanding here? Is what I
want to do at all possible?
If I can have a reference to a datatype or structure, why shouldn't I
have a reference to a pointer, which is also a datatype?
Thanks,
Roger
I have a bit of a strange problem here. This code works OK:
class foo
{
public:
foo(int **ref):
m_ref(ref)
{}
private:
int **m_ref;
};
int main()
{
int i = 1;
int *iptr = &i;
foo bar(&iptr);
return 0;
}
but this code does not:
class foo
{
public:
foo(int& *ref):
m_ref(ref)
{}
private:
int& *m_ref;
};
int main()
{
int i = 1;
foo bar(&i);
return 0;
}
I get this error when I compile it:
$ g++ -o test test.cc
test.cc:4: error: cannot declare pointers to references
test.cc:8: error: cannot declare pointers to references
This is just a testcase. From another compiler error, it looks like
the compiler (GCC) thinks that the prototype for foo::foo() is
actually foo::foo(int*), not foo::foo(int&*).
The reason I want this behaviour is as follows: I have a database
"transaction object" representing a transaction. It is allocated on
the heap with new, and passed to the constructors of several classes
which are instantiated by the parent class (and so on, recursively).
All the classes are part of the same database transaction, and so need
access to the same transaction object.
Now, I can't pass the object by value as a reference: it's perfectly
valid for it to be NULL, which causes transactions to be dynamically
created as required, rather than using the user-supplied one. It's
also valid for a child to delete the transaction object and/or create
a new one, in which case I want the pointer in the parent and other
children to be updated. This won't happen if I just pass a plain
pointer, since the pointer is passed by value. I could implement this
as a pointer to a pointer (see top example), but it seemed cleaner as
a reference to a pointer, since all the children could treat it as a
plain pointer, without having to dereference it twice.
Could anyone point out what I am misunderstanding here? Is what I
want to do at all possible?
If I can have a reference to a datatype or structure, why shouldn't I
have a reference to a pointer, which is also a datatype?
Thanks,
Roger