Aliases

Discussion in 'Programmer's Corner' started by mentaaal, Sep 18, 2008.

  1. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Hey guys,
    I was just wandering, why is an alias created like this:

    Code ( (Unknown Language)):
    1. int a = 0;
    2. int &b = a;
    Howcome this does not assign the value of a, currently 0 to the address of b?

    Howcome to assign an alias it is not done like this:

    Code ( (Unknown Language)):
    1. int a = 0;
    2. int &b = &a;
    This makes more sence to me anyway as the address one one gets assigned the address of the other?

    Also another wee problem i am having i am trying to figure out how you can assign a string literal to a character array which is a member of a class. I tried it in the code below but it returns an error below. Could anyone show me how to do this?

    Code ( (Unknown Language)):
    1. #include <iostream>
    2. using namespace std;
    3.  
    4. int main()
    5. {
    6.    
    7.     struct test
    8.     {
    9.        
    10.         char a[50];
    11.     };
    12.     test stest;
    13.     stest.a = "hello.";
    14.     return 0;
    15. }
    Code ( (Unknown Language)):
    1. 1>------ Build started: Project: Test7, Configuration: Debug Win32 ------
    2. 1>Compiling...
    3. 1>Test7.cpp
    4. 1>z:\college\year2\programming\programs\test7\test7\test7.cpp(13) : error C2440: '=' : cannot convert from 'const char [7]' to 'char [50]'
    5. 1>        There is no context in which this conversion is possible
    6. 1>Build log was saved at "file://z:\College\Year2\Programming\Programs\Test7\Test7\Debug\BuildLog.htm"
    7. 1>Test7 - 1 error(s), 0 warning(s)
    8. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    9.  
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    Those are compiler issues. The person/company that wrote the compiler determined the rules for how input statements are parsed and interpreted. A different language might either not have the ability to alias variables or do so using differing notation.

    You may not find the function as making sense to you, but the compiler determines how you have to use it. Sometimes it can be irritating enough that you find another language to use that makes more sense (or at least doesn't irritate you every time you write code).
     
  3. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    Alias or reference as I know it, is assigned just like normal variable assignment. So, the code:

    int &a = &b;

    is the correct usage.

    Note that it is different in the case of pointer, which would be:

    int *a = &b;

    The reference to a variable is used in codes just like the variable itself. It is usually benefits the program in parameter passing. Declaring a parameter to be passed by reference:

    Code ( (Unknown Language)):
    1.  
    2. int add(int &a, int &b)
    3. {
    4.    return a*b;
    5. }
    6.  
    7. int main(void)
    8. {
    9.   ...
    10.   result = add(&num1, &num2);
    11.   ...
    12. }
    13.  
    Note that the variable a and b are accessed by normal variable name in the function, but declared as passed by reference in the function declaration. The function is called by passing the references to num1 and num2, however some compilers will accept variable names without the reference designator as well.

    Assigning a partial string, or static initial value, to a member of a union or class is possible only during initial construction. If you declare a constructor, you could pass the initial value and assigned it to the member variable. Outside of that, you could use string copy to do that or you could try assigning a full string (50 characters) and see what happens :)
     
  4. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Thanks for the replies guys. Yeah i can see what you mean about the compiler determining it but it just seems so counter intuitive that i could see no logic in it at all.

    And n9352527, the error I get when doing it like "int&a = &b" is:
    1>z:\college\year2\programming\programs\test7\test7\test7.cpp(8) : error C2440: 'initializing' : cannot convert from 'int *__w64 ' to 'int &'


    Apologies i should have specified, i am using microsoft visual 2005 c++.

    String copy you say... right i will give that a shot.

    Cheers
     
  5. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    I don't think so, but if you can prove me wrong, I'm happy to listen. Here is some code taken from Bjarne Stroustrup's The C++ Programming Language, Second Edition, in the section on references:
    Code ( (Unknown Language)):
    1.  
    2. int i = 1;
    3. int& r = i; // r and i now refer to the same int
    4. int x = r;  // x = 1
    5. r = 2;      // i = 2
    6.  
    Mark
     
  6. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    At the risk of possibly saying again what someone else in this thread has said, what you're dealing with here is C++ syntax for a reference. You're looking at this from a C perspective, which doesn't apply. You cannot declare something to be of type "int &" in C.

    References are a bit like pointers, but it's probably not helpful to think of them this way, because how they work is implementation-dependent. IOW, one compiler vendor might implement them in one way, while another is free to implement them in a different way. I'm pretty sure that the code above won't work, but I don't have the time right now to try it out.

    I'm coming in a bit late on this thread, but I think someone else told you how to do what you need to do here. The reason you can't do the assignment above is that what you're trying to do is assign the address of a string constant ('const char [7]') to a variable that is of type array of 50 chars ('char [50]').
    Hope that helps.
    Mark
     
  7. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Hey Mark, yeah your thoughts are always greatly appreciated. Ha ha just so you know i have never ever looked at c, only c++. I just found it to be very strange when looking back at aliases, because when I see int &b = c, I see the address of b gets the value inside the variable c. I guess every language has its quirks.
     
  8. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    Yeah, my bad. I meant to say that the OP code is the correct one. Should've just copied the code over.