![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: May 2005
Posts: 60
Rep Power: 4
![]() |
Pointers to return multiple values
I'm steadily progressing through C++ and am looking at the moment at Pointers vs. References. I have a little question and turned here for it to be answered ...
#include <iostream>
using namespace std;
short Factor(int n, int* pSquared, int* pCubed);
int main()
{
int number, squared, cubed;
short error;
cout << "Enter a number (0 - 20): ";
cin >> number;
error = Factor(number, &squared, &cubed);
if (error)
{
cout << "Number: " << number << endl;
cout << "Square: " << squared << endl;
cout << "Cubed " << cubed << endl;
}
else
cout << "Error encountered!!" << endl;
return 0;
}
short Factor(int n, int *pSquared, int *pCubed)
{
short Value = 1;
if (n > 20)
Value = 0;
else
{
*pSquared = n*n;
*pCubed = n*n*n;
}
return Value;
}My problem arizes because I'm used to seeing things (such as pointers) clearly initialized, yet here ... I can't seem to see line that initializes the pointers that are used. I can see they're mentioned in the prototype of Factor, but that doesn't initialize them as usable pointers that can be legitimately used from there on ... does it? Even so, I can't see a place where the pointers are tied to gether with the 'squared' and 'cubed' variable integers anyway. I'd be pleased with a short, clear and concise explanation if possible. |
|
|
|
|
|
#2 | |||
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
short Factor(int n, int* pSquared, int* pCubed);
int main()
{
...
error = Factor(number, &squared, &cubed);
...
}
short Factor(int n, int *pSquared, int *pCubed)
{
short Value = 1;
if (n > 20)
Value = 0;
else
{
*pSquared = n*n;
*pCubed = n*n*n;
}
return Value;
}Quote:
Quote:
Quote:
ps: check out this thread |
|||
|
|
|
|
|
#3 |
|
Programmer
Join Date: May 2005
Posts: 60
Rep Power: 4
![]() |
Thanks, for that. A new issue has arrived though.
You might want to bear in mind that this is the first time I'm being ontroduced to copy constructors. // Passing Objects by Reference
#include <iostream>
using namespace std;
class SimpleCat
{
public:
SimpleCat (); // constructor
SimpleCat (SimpleCat&); //copy consructor (why have SimpleCat& inside the brackets? Does the & do anything)
~SimpleCat(); // destructor
};
SimpleCat::SimpleCat()
{
cout << "Simple Cat Constructor..." << endl;
}
SimpleCat::SimpleCat(SimpleCat&)
{
cout << "Simple Cat Copy Constructor.." << endl;
}
SimpleCat::~SimpleCat()
{
cout << "Simple Cat Destructor.." << endl;
}
SimpleCat FunctionOne (SimpleCat theCat); //
SimpleCat* FunctionTwo (SimpleCat *theCat); //
int main()
{
cout << "Making a cat..." << endl;
SimpleCat Frisky;
cout << "Calling FunctionOne..." << endl;
FunctionOne(Frisky);
cout << "Calling FunctionTwo..." << endl;
FunctionTwo(&Frisky);
char response;
cin >> response;
return 0;
}
// FunctionOne passes by value
SimpleCat FunctionOne(SimpleCat theCat)
{
cout << "Function One. Returning..." << endl;
return theCat;
}
// FunctionTwo passes by reference
SimpleCat* FunctionTwo(SimpleCat *theCat)
{
cout << "Function Two. Returning..." << endl;
return theCat;
}Now the output SHOULD, in theory, be ... Making a cat ... Simple Cat Constructor... Calling Function One... Simple Cat Copy Constructor.. FunctionOne. Returning... Simple Cat Copy Constructor... Simple Cat Destructor... Calling Function Two... Function Two. Returning... Simple Cat Destructor... Now, would someone please be kind enough tell me what is going on and the basic message of this example that I need to understand. Thanks in advance. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
A pointer is a reference in the dictionary sense, as is a nickname. In C++, the term "reference" was chosen (perhaps unwisely) to indicate an alias, and not just any old reference. A pointer is a separate variable which contains the address of a thing. I have a tutorial regarding pointers at the link in my signature. It does not cover references. A reference (C++ sense) is an alias, merely another name for an existing item. When you deal with a pointer, you deal with an item indirectly. When you deal with a reference, you deal with an item directly, but you call it "redneck" instead of "Jim Bob." When you copy an object you might get by with a "shallow" copy, which means it might contain pointers to other objects and you just copy those pointers. If the referred to object is deleted, all pointers to it become invalid. A non-shallow copy might make a copy of the objects and assign them new pointers. That way, if the original were destroyed, there would still be a valid object for the copy.
The use of the "&" has to be derived from context (in C++). It might be used as the "address of" operator and it might be used to assign an alias to an existing object. When you pass a reference to a function, the parameter is actually a pointer to the object, but when you pass a pointer you are actually creating a copy of a pointer. The effective references differ by one level of indirection. The compiler takes this into consideration so that you use one directly (syntactically speaking) inside the function and the other indirectly. I'm not sure if this addresses your question. Post back if not. A pointer is a reference, but not a "C++ reference." A convict and his number are not the same, but "Bent Nose" Billy the Kid and William Bonney might very well be.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Programmer
Join Date: May 2005
Posts: 60
Rep Power: 4
![]() |
Thanks, that helps. One other thing I was worried about was that I know references can not be null. Some compilers might still compile it, but if a nulled reference is used, it will cause a problem.
My question is how do you create a null reference, so I know what they are. Simple assigning a reference to a variable containing the null number (0) doesn't crash the system, like I was hoping. This may seem odd, but my logic is if I know how to make a null referece, I'll know what they are .. and be able to stop them from occuring. |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
Example you posted definitely does not demonstrate purpose and usefullness of a copy constructor.
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() |
#include <iostream>
int main(int argc, char *argv[]) {
int *pntr = NULL;
*pntr = 82;
std::cout << pntr << std::endl;
return 0;
}I think the code above will throw an error.
__________________
|
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Tempest's example doesn't even contain a reference. A reference (C++) can't be NULL because you make a reference by saying "ThisName = &ThatName". If you assign zero to ThisName you just change the value of ThisName AND ThatName (the same bloke, you see), you don't create a null reference.
A null pointer is another horse. Whether or not a null pointer causes a problem depends upon the circumstance. See the last section of my tutorial for material on NULL pointers.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Programmer
Join Date: May 2005
Posts: 60
Rep Power: 4
![]() |
Thanks for that help.
I think I understand the fundamentals of pointers and references. At the very least I can interpet most pieces of code using them now. I've been on them for a while, what I think I need to do is take a break. I'll finish the rest of my book, and then go over in detail the pointer and reference chapters again. After a small break I think I'll be able to go back and tackle the tougher issues better than I am able to now, anyway. Once again, thanks for the help. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|