Use of * and &? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
For this function TreeNode* buildTree(vector& preorder, vector& inorder). Why there is a * in front of the function name? Why they use & in front of the variables?

Try looking at * not as being in front of function name, but rather as being behind TreeNode. In other words, that * changes function return type from structure to pointer to a structure.
& in front of variables means that they are, essentially, still pointers, but they can't be NULL (there are ways to get around that, but that would just confuse the issue further). So vector& preorder means that preorder is pointer (the term used is reference) to vector, but you don't need to check if that pointer is NULL. You can simply go ahead and use it.
Without much loss you could change those & into *, changing . in your function to -> and things would work the same. I bet assembly code would be exactly the same as well.

Related

Best way to call a function C++ [closed]

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 5 years ago.
Improve this question
Calling functions into another function or even main is what my question is about.
One way to call a function is :
callOne(1, 2);
Another way to call a function is :
callTwo = func2(5, 9);
Please explain what is the difference? Is one way better than the other? Which way is most encouraged to use?
It depends on the return value of the function.
If this is void the function does not have a return value and you do the first.
callOne(1, 2);
If it has something else like int, char or std::string you can use the first but also the second.
callTwo = func2(5, 9);
In this case you save the value the function returns in the variable.
If you use the first variant on a function with return value (not void) the result will be ignored.
I would advice you to google for some basic tutorials like this or this for example. You won't be very fast if you let SO teach you basic things.

Style for naming a function that is setting an argument by reference? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
So pretend I'm developing a car class and I want one of the car class's functions to return a list of passengers, except I'd like to put a list reference as an argument and just set that list instead of returning a list.
void GetPassengerList(PassengerList &passengerList); //sets the list
I don't know if I should call it GetPassengerList or SetPassengerList, or something else. I feel like using the words get / set make it seem like there is a private variable that is being manipulated like the typical getter / setter methods. What's a good naming convention to use here?
In our team for input/output arguments we either use
void AdjustPassengerList(PassengerList&);
or
void AddPassengersTo(PassengerList&);
Depending on the use-case. For example the first one could be used if you want a list created from more than one car. The second usually reads well in code, something like:
car.AddPassengersTo(list);

Declaring certain elements as const in an array c++, sudoku solver [closed]

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
Is it possible for me to take an existing array and make certain values (not all) constant? I'm trying to build a sudoku solver and my idea is to have the user enter values, so I would like to have those values remain constant as I change the empty spaces. Any tips or advice with the solver would also be appreciated. This is my first quarter working with c++. Thanks!
No. Const is essentially a compile-time idea. It's not something that can be toggled as a program is running.
If you need certain values to remain untouched while others are changed, then you need to put that into your data and logic. For instance, each value might have an associated boolean that indicates whether or not it can be changed. Then write your logic to respect that boolean.

Using a Linked List or Array for Monopoly? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm making a Monopoly game for a school project, written in C++. The first thing I'm working on is implementing the board. It's intuitive to me that each tile will be an object holding information and functions and whatnot, but I cannot decide whether these should be contained in a linked list or array.
It made sense for a linked list because i could simply have the last tile point to the first, but it also seems more efficient to use an array since I can instantly access Tile[5] for example.
Can anybody clarify as to which would be better for this purpose?
It's a fixed size. That negates about 90% of the advantages of a linked list.
You will NEVER be accessing it sequentially (unless, instead of dice, everybody just moves one square each time), but always randomly. That's about 90% of the advantage of an array.
The one reason you cite for using a linked-list is trivially handled differently. (new_position = (current_position + roll) % 40;)
Therefore: You unquestionably want to use an array.

C++ Pointer to Pointer [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Im trying to do a pointer to pointer to a dynamic array that his elements will be a pointer to "figurasGeom"
How can I do it? I started with the pointer to pointer but Im not sure if that is ok..
figuraGeom ** lista;
Is there another way to make a dynamic array without using "vector"?
I have something like this but I dont know how to implement it
figuraGeom* vectr [];
Thank you in advance!
Based on your description, you left the vector part out, so you would need something like this:
vector<figuraGeom*> **lista;
But this is a really weird data representation, at least in C++. You should start working more with higher-level containers and/or smart pointers in C++.