This question already has answers here:
What are the pointer-to-member operators ->* and .* in C++?
(7 answers)
Closed 7 years ago.
Good day,
I've come across this question, but I'm specifically interested in the "object pointed to by member ..." type operators as listed here on Wikipedia.
I have never seen this in the context of actual code, so the concept appears somewhat esoteric to me.
My intuition says they should be used as follows:
struct A
{
int *p;
};
int main()
{
{
A *a = new A();
a->p = new int(0);
// if this did compile, how would it be different from *a->p=5; ??
a->*p = 5;
}
{
A a;
a.p = new int(0);
// if this did compile, how would it be different from *a.p=5; ??
a.*p = 5;
}
return 0;
}
But this doesn't compile because p is undeclared. (See example)
Could anyone please provide a real-world example of the use of operator->* and/or .* in C++?
Those operators are used for pointer-to-member objects. You won't come across them very often. They can be used to specify what function or member data to use for a given algorithm operating on A objects, for instance.
Basic syntax:
#include <iostream>
struct A
{
int i;
int geti() {return i;}
A():i{3}{}
};
int main()
{
{
A a;
int A::*ai_ptr = &A::i; //pointer to member data
std::cout << a.*ai_ptr; //access through p-t-m
}
{
A* a = new A{};
int (A::*ai_func)() = &A::geti; //pointer to member function
std::cout << (a->*ai_func)(); //access through p-t-m-f
}
return 0;
}
The ->* and .* syntax is the "pointer-to-member" operator that can be used to store pointers to members of a very specific object.
Usage example:
class A {
public: int x;
};
int main() {
A obj;
int A::* memberPointer = &A::b; //Pointer to b, which is int, which is member of A
obj.*memberPointer = 42; //Set the value via an object
A *objPtr = &obj;
objPtr->*memberPointer = 21; //Set the value via an object pointer
}
Related
This question already has answers here:
Type qualifier appear in multiple item declaration (exhibiting strange behavior with pointer)
(2 answers)
Closed last month.
I know
int* p, k;
declares k to be int and p to be int*, but how about:
static int a, b;
const int c, d;
and other declarators. Is it the same for all of them? Obviously for pointers, arrays, and indirection it would be.
I'm not asking if it compiles; I'm asking for rules regarding these declarations, preferably quoted from the standard.
It doesn't take much to test this. The following does not compile because p has been declared const. https://godbolt.org/z/P7bj85aWx
int main() {
const int k = 1, p = 5;
p++;
return 0;
}
For static, consider the following which outputs 67 rather than 66. The latter would be expected if b were not static.
https://godbolt.org/z/E58azYdfe.
#include <iostream>
int foo() {
static int a, b = 5;
b++;
return b;
}
int main() {
std::cout << foo() << foo() << std::endl;
return 0;
}
I got a situation where I have a class B having a protected array of A*. I have an another function A* func() which returns pointers to A objects stored in some other container. (for example std::map<int,A*> or A* arr[]).
For example the following implementation:
#include <iostream>
using namespace std;
class A {
public:
A(int _x): x(_x){};
int x;
};
class B{
public:
B() {
this->arr[0] = new A(1);
this->arr[1] = new A(2);
}
// use idx to index in to the internal array and return A*
A*& operator[](int idx); // return reference to pointer to avoid not a lvalue error
private:
// class B has this private array of A*
A* arr[2];
};
A*& B::operator[](int idx) {
return this->arr[idx]; // 0<=idx<=1
}
A* arr[2];
A* func(int x) {
return arr[x]; // 0<=idx<=1
}
int main() {
arr[0] = new A(12);
arr[1] = new A(14);
B b;
b[1] = func(1); // assign a new value
cout << b[1]->x << endl; // prints 14
}
// memory leaks of this program can be removed using `std::shared_ptr<A>` in the place of `A*`.
In this example, operator[]() returns a reference to a pointer, because returning pointer becomes rvalue and I will not be able to assign value to it (i.e. b[1] = func(1)).
My question is, is this reference to pointer returning recommended (other ways to achieve this )? or is this perfectly fine with raw pointers as well as with shared_ptr?
Thank You!
I know that in C++, you use either -> or :: instead of . in language such as C# to access object's values, e.g. button->Text or System::String^, but I don't know when I should use -> or ::, and it is very frustrating as it causes me many compiler errors. I would be very grateful if you could help. Thanks :)
-> is when you are accessing the member of a pointer variable. EG: myclass *m = new myclass(); m->myfunc(); Calls myfunc() on pointer to myclass. :: is the scoping operator. This is to show what scope something is in. So if myclass is in namespace foo then you'd write foo::myclass mc;
-> if you have pointer to some object this is just shortcut for dereferencing that pointer and accessing its attribute.
pointerToObject->member is the same as (*pointerToObject).member
:: Is for access stuff from some scope - it works only on namespaces and class/struct scopes.
namespace MyNamespace {
typedef int MyInt;
}
MyNamespace::MyInt variable;
Contrary to what your question states, you do use . in C++. Quite a bit.
. (used with non-pointers to access members and methods)
std::string hello = "Hello";
if (hello.length() > 3) { ... }
-> (used with pointers to access members and methods)
MyClass *myObject = new MyClass;
if (myObject->property)
myObject->method();
:: (scope resolution)
void MyClass::method() { ... } //Define method outside of class body
MyClass::static_property; //access static properties / methods
:: is also used for namespace resolution (see first example, std::string, where string is in the namespace std).
I try to show an examples of some usages of ::, . and ->. I hope it helps:
int g;
namespace test
{
struct Test
{
int x;
static void func();
};
void Test:: func() {
int g = ::g;
}
}
int main() {
test::Test v;
test::Test *p = &v;
v.x = 1;
v.func();
p->x = 2;
p->func();
test::Test::func();
}
Opertor -> is applied when the left operand is a pointer. Consider for example
struct A
{
int a, b;
A( int a, int b ) : a( a ), b( this->a * b ) {}
};
Operator :: referes to the class or anmespace for which the right operand belongs. For example
int a;
strunt A
{
int a;
A( int a ) { A::a = a + ::a; }
};
The period is used then the left operand is lvalue of an object. For example
struct A
{
int x;
int y;
};
A *a = new A;
a->x = 10;
( *a ).y = 20;
I'm pretty new to C++ and as an exercise (and perhaps eventually .Net utility) I'm doing a pointer wrapper (actually in C++/CLI, but this applies to C++ as well). This pointer wrapper (called Apont) currently behaves just like a pointer would, as the test below can show, if lines marked 1. and 2. are commented out:
int main(array<System::String ^> ^args)
{
double ia = 10; double ip = 10;
double *p = &ip; // pointer analogy
Apont<double> ^a =
gcnew Apont<double>(ia); // equivalent to what's below, without errors
a = ~ia;/* 1. IntelliSense: expression must have integral or unscoped enum type
error C2440: '=' : cannot convert from 'double' to 'Utilidades::ComNativos::Apont<T> ^'
error C2171: '~' : illegal on operands of type 'double'*/
Console::WriteLine("ip = {0}; *p = {1}; ia = {2}; !a = {3}", ip, *p, ia, !a);
ia = 20; ip = 20;
Console::WriteLine("ip = {0}; *p = {1}; ia = {2}; !a = {3}", ip, *p, ia, !a);
*p = 30; // pointer analogy
a->Valor = 30; // does exacly what's below, without errors
!a = 30;/* 2. IntelliSense: expression must be a modifiable lvalue
error C2106: '=' : left operand must be l-value */
Console::WriteLine("ip = {0}; *p = {1}; ia = {2}; !a = {3}", ip, *p, ia, !a);
//a->Dispose();
Console::ReadKey();
p = nullptr;
return 0;
}
There are two things I don't like here, marked with 1. and 2. in the code comments, before the lines with errors. The operator~ (see 1.) is defined outside Apont, below:
template<typename T> static Apont<T>^% operator ~(T& valor)
{
return gcnew Apont<T>(valor);
}
I think this one has to be defined outside Apont, but I'm not sure. I cannot understand very well the errors it produces (these are, of course, in the use, not in the definition).
To set the value to which the instance of Apont refers I must use a property (the line marked 2. doesn't work, with errors in the setting usage only), Apont::Valor, which is the equivalent to use *p. What I'd like to do is as I use *p to get or set the value it points to, use !a with the same effect on Apont. Here's Apont::operator!()'s current definition:
T operator !()
{
return Valor;
}
As you can see in 2. (comment in the code, before the respective errors), it doesn't work for setting a value. Maybe I should return a reference? Make another operator with the same name, perhaps outside the class? I tried several options, however, I got similar errors, and came out more confused.
The question is: how can I make an operator that behaves like & (in this case, ~) and one that behaves like * (in this case, !, for dereference, but that behaves like Apont::Valor, whose old definition you can see below)?
property T Valor
{
T get()
{
if (pointer != nullptr)
return *pointer;
else if (eliminado && ErroSeEliminado) // means "disposed && ErrorIfDisposed"
throw gcnew ObjectDisposedException("O objeto já foi pelo menos parcialmente eliminadao.");
else if (ErroSeNulo) // means "ErrorIfNull"
throw gcnew NullReferenceException();
else
return 0;
// don't worry, this is not default behavior, it is returned only if you want to ignore all errors and if the pointer is null
}
void set(T valor)
{
*pointer = valor;
}
}
Let me recap in a new answer for clarity.
Solving the ! operator is easy, as I said in my previous answer, just add a reference.
So for the operator ~, the goal was to have it behave like the & operator and call the constructor of the pointer wrapper class.
I don't think that is possible. It is certainly possible for user defined objects, but I don't think it is possible to overload unary operators for builtin types. So there are three solutions depending on what you prefer:
The first one does exactly what you want, but will break for primitive types:
#include <iostream>
template<typename T>
struct A {
T* payload;
A()
: payload(NULL){}
A(T *ptr)
: payload(ptr) {}
T& operator !(){
return *payload;
}
};
// this will not work for primary types
template<typename T>
A<T> operator ~(T &b){
return A<T>(&b);
}
struct B{
int test;
};
int main(){
B b; b.test = 4;
A<B> a;
a = ~b; // I think this is what you want
std::cerr << (!a).test << std::endl;
// this does not work
//int i = 4;
//A<int> a;
//a = ~i;
}
Second solution: use a compound assignment operator. Pros are the side effects are minimal, cons is this is not very intuitive and might break the nice design you had in mind.
#include <iostream>
template<typename T>
struct A {
T* payload;
A() : payload(NULL){}
T& operator !(){
return *payload;
}
};
template<typename T>
A<T>& operator &=(A<T> &a, T& b){ // should be friend of the above
a.payload = &b;
return a;
}
int main(){
int i = 3;
A<int> a;
a &= i;
std::cerr << !a << std::endl;
}
Third solution: overload the basic assignment operator. This is more intuitive to write but has a lot of side effects:
#include <iostream>
template<typename T>
struct A {
T* payload;
A() : payload(NULL){}
T& operator !(){
return *payload;
}
A<T>& operator = (T & b) {
payload = &b;
return *this;
}
};
int main(){
int i = 3;
A<int> a;
a = i;
std::cerr << !a << std::endl;
}
Someone might have a solution to hijack the operators for primitive types, but i can't think of any simple solution.
If i understood your code correctly, you want the operator ~ to return a copy of the pointer wrapper and the operator ! to act as dereference?
In this case, you can define the unary operator ~ inside the Apont class which calls a copy constructor. And the operator ! has to return a reference indeed if you want to asign a value.
I think the following c++ code defines what you want to do (I renamed Apont to A):
#include <iostream>
template<typename T>
struct A {
T* payload;
A(T *ptr)
:payload(ptr) {}
A(const A&other)
:payload(other.payload) {}
T& operator !(){
return *payload;
}
T* operator ~(){
return payload;
}
};
int main(){
#define PRINT(X) std::cerr << #X << " = " << X << std::endl
int i = 0;
PRINT(i);
A<int> a(&i);
!a = 1;
PRINT(i);
A<int> b = ~a;
!b = 2;
PRINT(i);
}
The output of the code above is:
i = 0
i = 1
i = 2
According to your comments, you said you wanted the operator ! to behave exactly like the wrapped pointer. You can do so, but then the syntax changes and you need to dereference it to assign a new value (because it is a pointer...). ie something like:
#include <iostream>
template<typename T>
struct A {
T* payload;
A(T *ptr): payload(ptr) {}
// this now behaves like accessing the wrapped pointer directly
T*& operator !(){
return payload;
}
};
int main(){
#define PRINT(X) std::cerr << #X << " = " << X << std::endl
int i = 0;
int j = 999;
PRINT(i);
A<int> a(&i);
*(!a) = 1; // note the change of syntax here
PRINT(*!a); // and here
!a = &j; // but now you can change the wrapped pointer through the operator
PRINT(*!a);
}
class A {
public: int i;
};
A *a = new A();
How to get the address of a->i? I tried &a->i and also &(a->i) but those generate compile time errors:
"left of '.i' must have class/struct/union type"
You have not provided the same code you tried to compile. Always copy and paste. The tells in your code are that you don't have a syntactically correct class declaration or variable declaration, and that your error message talks about ".i" when you've claimed you've only used a->i. Here's working code:
#include <stdio.h>
class A {
public:
int i;
};
int main() {
A* a = new A();
int* i = &a->i;
printf("a: %p\na->i: %p\n", a, i);
return 0;
}
Ultimately, the syntax you say you tried for getting the address of the member was correct. The syntax the error message says you tried was a.i. That doesn't work, and for the reason the error message gave. The variable a is not a class, struct, or union type. Rather, it's a pointer to one of those types. You need to dereference the pointer to get at the member.
When I run it, I get this:
$ ./a.out
a: 40748
a->i: 40748
The addresses are the same because A is a simple class, so this output is to be expected. The first member is frequently placed at the very start of a class's memory. Add a second member variable to the class and get its address; you should see different values then.
In c++:
class A {
public: // note colon after public
int i;
}; // note semicolon after bracket
A *a = new A(); // note *a not a*
to print:
cout << ios::hex << &a->i << endl;
For me this seems to work ok.
&a->i should work. In your case since the class just has one public integer both the address of a and i will be the same.
You're on the right track, but from the compile error you mention, it sounds like you were typing "&a.i"
class A
{
public:
int i;
};
...
A *a = new A();
int *i = &a->i;
This code appears to compile?
class A {
public:
int i;
};
int main() {
A *a = new A();
int *x = &a->i;
return 0;
}
The following works for me using g++
class A
{
public:
int m_i;
};
int
main()
{
A* a = new A();
int* i_ptr = &(a->m_i);
return 0;
}
I am guessing you mean A* a = ... and not A a* = ...