![]() |
String argument
I have this class 'CMatrix':
:
class CMatrixAnd I want to pass a string to the constructor... but my compiler says "string cannot start a parameter declaration"... What's wrong? Thanks in advance ;) |
The code shown compiles without error, for me, using VC++ 6.0 and VC++ 2005. I didn't try Borland. I did comment out "CVector *matrix;" rather than worry about its definition. I also can't find an error that conforms to your wording, by that could be different compilers. Does the error actually occur/point at what you are showing?
|
Nevermind... since my prog is in various modules, I forget to put in the 'CMatrix.h':
:
#include <iostream>Thanks for the time lost... ;) |
and this raises a question from me!
:
CMatrix(string&) {;}please explain to me in psuedocode what happens when you write the ampersand AFTER the variable type and pass it to a constructor? personally i would have just wrote :
CMatrix(string);i guess my questions are! First: why write the ampersand after the variable type? and Second:how would your program benefit from this? |
In C++, as shown, the ampersand represents a "reference". That's probably an unfortunate choice of terms by the language writers, as pointers, your pet name for your sweetheart, plain labels, and other items are still technically (and correctly designated as) references. Nevertheless, it's a special thangy in C++. It is, in fact, an alias. It is just another name for the original, not a copy, not an address-book entry, or any of that. When a reference is passed to a function, the argument is actually a pointer of a sort, but there is one less level of indirection. Consequently, usage inside the function has a different syntax. When you dink with the reference, you modify the original, just as you do when you mess with a pointer's target, but, as I say, with one less level of indirection. You may go to my pointer tutorial if you like, but I promise you, you'll not find them covered there :p .
|
ok yes im familiar with reference's. I just have never seen them refered to with the ampersand after the variable type. With functions and parameters aside, when i refer to a reference would i always place the ampersand after the variable name? May i see some examples of this :)
|
:
myFunction (mytype& label) |
lol wierd, i will stick with my pointers :)
|
| All times are GMT -5. The time now is 11:10 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC