473,378 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

de-reference a pointer to object

Hi

class myType{
public:
void doit();
std::string x;
};

int main() {
myType* mt;
mt->doit();
(*mt).x = 4;
}

is there a reason why it is better not to use
(*mt).doit();
mt->x = 4;
instead?

thanks

Dec 7 '06 #1
3 1433
Gary Wessle wrote:
Hi

class myType{
public:
void doit();
std::string x;
};

int main() {
myType* mt;
mt->doit();
(*mt).x = 4;
}

is there a reason why it is better not to use
(*mt).doit();
mt->x = 4;
instead?
Why would you bother dereferencing the pointer and then using dot
operator instead of just using the pointer notation - ?

There should be no noticable difference in the effect of the two
statements but the pointer notation is more clear.

-
Ivan
http://www.0x4849..net

Dec 7 '06 #2
Gary Wessle wrote:
Hi

class myType{
public:
void doit();
std::string x;
};

int main() {
myType* mt;
mt->doit();
(*mt).x = 4;
}

is there a reason why it is better not to use
(*mt).doit();
mt->x = 4;
instead?

thanks
To me, mt->whatever reads better. I tend to read it as "mt pointing at
whatever", making it clear that mt is a pointer (or pointer-like object).

But don't mix the two notations. It leads to confusion. Either stick
with (*mt).whatever everywhere or go with mt->whatever. You'll find
that most people us the second notation.
Dec 7 '06 #3

Gary Wessle wrote:
Hi

class myType{
public:
void doit();
std::string x;
};

int main() {
myType* mt;
mt->doit();
(*mt).x = 4;
}

is there a reason why it is better not to use
(*mt).doit();
mt->x = 4;
instead?
What is "better" is never, ever access a naked pointer as if it was
actually pointing to something.
The code in main() above:

myType* mt;
mt->doit(); // UB

is undefined behaviour ( should be "dangerous behaviour" (c) ). mt is
an invalid pointer to nothing. A big fat no-no and so nasty its beyond
any description. I strongly suggest you label pointers appropriately
and initialize them to prevent UB.
At least the compiler will generate an error if you attempt to access
an invalid pointer that way.

int main() {
myType* p_mt = 0; // nullptr initialized
myType myinstance;
p_mt = &myinstance; // ah! now we are in business
p_mt->doit();
mt->x = 4;
}

a much better alternative to any of the above is:

int main() {
myType myinstance;
myType& r_mt = myinstance; // a reference
r_mt.doit();
r_mt.x = 4;
}

And perhaps the ptr vs reference semantics might also help you decide
which is better: -for pointers vs . for objects and references.

Dec 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: WebRod | last post by:
Bonjour, comment savoir en moins de 5 secondes (enfin rapidement quoi) combien de nouveaux messages non récupérés se trouvent dans une BAL sur une serveur POP3 (pas d'imap donc)???? (en sachant...
2
by: grigoo | last post by:
bonjour a tous je me presente a vous::: greg dit le grigoo sur le web ,,etudiant en bioinformatique a montreal et jusqu au cou dans notre language prefere....java. et biojava.. et je suis en un...
0
by: Michael Dyson | last post by:
M. Michael DYSON DIRECTEUR-ADJOINT SOCIÉTÉ DE SÉCURITÉ SARL. TEL.00229 20 21 80 Cotonou République du Bénin Email:michaeldyson2005@latinmail.com Bonjour . Je sais que mon message sera d’une...
0
by: Programas de email | last post by:
Convidamos você a experimentar o Magic Seven, o melhor programa de envio de emails para listas de contatos. Com o Magic Seven você pode enviar emails personalizados e com recursos multimídia,...
0
by: Alejandro Scomparin | last post by:
FORMULACION, PREPARACION Y EVALUACION ECONOMICA Y FINANCIERA DE PROYECTOS DE TECNOLOGIA INFORMATICA (IT) en la UTN Inicia: 17 de Mayo. 19 hs Dirigido a Esta preparado para gerentes, jefes,...
0
by: Alejandro Scomparin | last post by:
FORMULACION, PREPARACION Y EVALUACION ECONOMICA Y FINANCIERA DE PROYECTOS DE TECNOLOGIA INFORMATICA (IT) en la UTN Inicia: 17 de Mayo. 19 hs Dirigido a Esta preparado para gerentes, jefes,...
0
by: gandalf | last post by:
Con motivo del enorme esfuerzo realizado para visitar la ciudad argentina de Córdoba, participar en la Reunión del MERCOSUR, en la clausura de la Cumbre de los Pueblos en la histórica Universidad...
1
by: crow | last post by:
http://www.pagina12.com.ar/diario/elpais/1-72984-2006-09-14.html Por Miguel Bonasso Desde La Habana Me había preparado para verlo, pero la realidad fue mucho más fuerte. Incluso le llevaba de...
1
by: gandalf | last post by:
CON LA PASION DE SIEMPRE HABLO DE CHAVEZ, DE LA MEDICINA CUBANA... Y DE SU PROPIA MUERTE Relato de la nueva gran batalla de Fidel El líder cubano mostró cómo evoluciona su recuperación en el...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.