Thread: a const question

  1. #1
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36

    a const question

    I am did something like this:

    struct myStruct {
    File *file
    .
    .
    .
    };

    status someMethod (const File *file, int a) {

    myStruct *struct01 = new myStruct;

    .
    .
    .

    struct01->file = file;

    .
    .
    .
    }

    Can I do something like that? The compiler gave an error if I do this. I tried with:

    struct01->file = (File*)file;

    and it works. Do I have to cast it to make it work?

    Thanks
    If only life is as easy as C...

  2. #2
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    This is a really tricky and subtle problem involving the const qualifier. Put simply, in the context of pointers, you can't assign a const pointer to a non const pointer without a cast but you can assign a non const pointer to a const pointer. The C++ standard explains it in the usual cryptic way, but this is pretty much what it boils down to.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Yes, but cast like this:

    Code:
    struct01->file = const_cast<File*>(file);
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36
    Yes, but cast like this:


    code:--------------------------------------------------------------------------------
    struct01->file = const_cast<File*>(file);
    --------------------------------------------------------------------------------
    Hmm..what does const_cast do?

    Is it specifically only for casting const data?
    If only life is as easy as C...

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    C/C++ is a case sensitive language...try this (I haven't tested it, but it has a good chance...)
    Code:
    struct myStruct {
    FILE *file
    .
    .
    .
    };
    
    status someMethod (const FILE *file, int a) {
    
    myStruct *struct01 = new myStruct;
    
    .
    .
    .
    
    struct01->file = file;
    
    .
    .
    .
    }
    Away.

  6. #6
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36
    C/C++ is a case sensitive language...try this (I haven't tested it, but it has a good chance...)
    Oops, I did made a mistake when posting the code to the forum, but I did used FILE in my code.
    If only life is as easy as C...

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by heljy
    Hmm..what does const_cast do?
    It is used to remove const or mutable specifiers from a type:
    Code:
    const int* a = 3;
    
    int* b = a; //Error
    int* c  = const_cast<int*>(a); //OK
    Last edited by Sang-drax; 09-08-2002 at 04:16 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36
    const int* a = 3;

    int* b = a; //Error
    int* c = const_cast<int*>(a); //OK

    That makes sense

    Ur previous

    int* c = const_cast<int>(a); //OK

    had me confused for a while
    If only life is as easy as C...

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    "edit post" is a great feature.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Need help implementing a class
    By jk1998 in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2007, 03:13 PM
  5. Replies: 6
    Last Post: 12-06-2005, 09:23 AM