Best Practices for Pointers and References

[Edit Note: as I made this is kind of mutated, so if there's more than two questions, or if it stays from what I told you I wanted in the beginning, please don't be frustrated because I'm still unsure about the matters talked about.]

Hello,

I have been learning C++ for a while now, but I have a long history of programming with other languages that make sure of garbage collection (e.g. C#, python).

The topic of the post is pointers and references. I understand, at least most, of how they both work, but for the sake of making sure that my understanding is correct here is a simple example.

Pointer Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main(int argc, char **argv[])
{
  // This is just a random variable I will be pointing to.

  int importantNumber = 25;

  // This is the pointer (int) variable that is pointing to "importantNumber"

  int *pImportantNumber = &importantNumber;

 // I know if I output this line of code below, that it will give me the actual number 25, which is stored in the (int) variable importantNumber

  cout << *pImportantNumber << endl;

 // I know the following two outputs will give me the memory address of where importantNumber is storing the value 25

 cout << pImportantNumber << endl;
 cout << &pImportantNumber << endl;

  return 0;
}  


To condense all of above:
There are two variable: importantNumber, which holds the value 25, and pImportantNumber which is a pointer, pointing to the variable importantNumber (25).

Later on in my learning, when classes were being made, and then later on being used as objects, forks my two main questions:

When you're creating an object of a class that you create, when is the best time to create a regular object variable, and when it is best to create a "regular" object variable?

Here is an example of both (The method sayHello just outputs 'hello' [imagination?]):
1
2
3
4
5
6
7
8
9
10
// This is the "regular" declaration of my fake class "TestClass".

TestClass test;
test.sayHello();

// This is creating a pointer object variable of "TestClass".

TestClass *ptest = new TestClass();
ptest->sayHello();
delete ptest;


The pointer variable "ptest" doesn't point to anything but newly allocated memory. Should I do this at all, or only use pointers to point to a reference?

My Second main question deals more with using pointers and references when creating methods for classes. In the same point and time in my learning the previous question arouse, I saw a lot of methods being made with constant reference parameters:

 
void saySomething(const char &word[]);


Why would I make a parameter constant, and why is it a reference?

When is the best time to make a parameter a pointer? (Example below)

1
2
3
4
// pointer parameter method
// p.s. Would I use "const" with this like the previous code?

void saySomething(char *word[]);


If you are reading still, I just wanted to extend my gratitude for your help. Any kind of informative response will be helpful. If you are able to add examples I would be extremely grateful!

Thanks again,

Chase
The pointer variable "ptest" doesn't point to anything but newly allocated memory.

the pointer variable "ptest" is not pointing "to newly allocated memory", it is pointing to the nameless object of type TestClass that new created for you (btw, the parentheses are redundant, it's new TestClass;)

Should I do this at all

No, as written, this is gratuitously exception-unsafe, and also performs completely unnecessary allocation/deallocation.

void saySomething(const char &word[]);

That is an error (cannot compile), but assume you're talking about something like void saySomething(const std::string& word);

Why would I make a parameter constant

That parameter is not constant itself, it is a reference to const. You do that when your function does not modify the object passed by reference.

, and why is it a reference?

In that case, to avoid making an unnecessary copy. Copying a string may take a long time, and if your function is only reading the characters, it may we well read from the original object.

When is the best time to make a parameter a pointer?

Very rarely. One semi-rare case is when the parameter is optional and the null pointer value is used to indicate that situation. For example, std::stoi takes a pointer as its second argument, which can be null.
Last edited on
My goodness. Thank you very much! That answered everything in a very clear way.

=)
Topic archived. No new replies allowed.