references

Greetings,

Could any one help me with this please:

void SetFileName (const G4String& name) { fileName[0] = name;};

This function gets the address of the first room of a string (name)as its argument and put the contents of that 1st room in to the first room of another string filename

I want to make sure if I am right or not!

many thanks in advance
closed account (zb0S216C)
Yes. The reference operator holds the address of the passed object[1]. Assuming that fileName is an array of G4Strings, your code works.

References:
[1]http://msdn.microsoft.com/en-us/library/w7049scy(v=vs.71).aspx
Hi bbcc,

I am a little puzzled as to what you are trying to do here.

You say that you want to copy the first ``room'' (element?) of the G4String name into the first room of another G4String filename. Therefore, name and filename are both G4String objects, and hence, the above is not what you want. As Framework says, this will only work...

Assuming that fileName is an array of G4Strings

the key word being ``Array'', which I gather is not applicable in this case.

It seems that you are trying to use reference operators to initialise a G4String, called filename, using another G4String, called name, and that you will later use filename to initialise an istream object bound to a data file.

If this is so, why not just initialse the istream object using a const char*:

1
2
const char* name = "path/to/my/file.txt";
ifstream infile(name);


All this is doing is passing the address of the first element of the char array to the ifstream constructor. In other words, the above is identical to this:

1
2
const char* name = "path/to/my/file.txt";
ifstream infile(&name[0]);


So you don't really need to worry about using & to construct istream objects since you can do:

 
ifstream infile("path/to/my/file.txt");


Hope that helps. Good luck!
closed account (zb0S216C)
Dangrr, fileName is clearly an array due to the fact that Bbcc uses a sub-script operator to access the first element. The first element of fileName is assigned the object referred to by name.

the key word being ``Array'', which I gather is not applicable in this case

Enlighten me.

Dangrr, have you ever considered that fileName is an array of G4Strings, and not an array of characters?
Last edited on
A reference refers to another variable - it's really that simple.

Normally, when you make a function call in C or C++, the arguments are passed "by value." That means that the function receives a COPY of the argument, not the actual argument used in the caller. Your SetFileName() function, however, passes the argument "by reference," which means that the variable in the function is the SAME as the one in the caller - no copy is made. When a function signature looks like this:

 
int func( SomeType& variable );  // Note the ampersand 


the argument "variable" is passed "by reference". With the following signature:

 
int func( SomeType variable );


the argument "variable" is passed "by value".

Hope that helps.
Last edited on
@Framework
Enlighten me.

Well, because bbcc says that name is a string and that filename is another string, implying that both objects are of the same type

a string (name)as its argument and put the contents of that 1st room in to the first room of another string filename


Apologies to bbcc if I misunderstood you.
Hello Buddies,

I would like to answer dangrr888 that I am trying to understand a previously written code and I just wanted to make sure if I am in the right track or not.

But thank you from you all.
Topic archived. No new replies allowed.