Help with overloa casting operator

This is a discussion on Help with overloa casting operator within the C++ Programming forums, part of the General Programming Boards category; I have Code: void* classname::data(){ //this function works fine return data; } classname::operator char*(){ //this one cause the program to ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    51

    Help with overloa casting operator

    I have
    Code:
    void* classname::data(){    //this function works fine
    return data;   
    }
    
    classname::operator char*(){   //this one cause the program to crash
    return (char*)(data());
    }
    what did I do wrong?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Impossible to tell without more code.
    Show the simplest possible compilable example that reproduces the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    what's the type of data?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    data is void* type

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    How are you setting data, and how are you using classname in a char* context? One way or another the two don't work together.

    In general you should avoid void * and C casts because they undermine the type system.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    this is what I have to do

    create :
    operator char*();

    Overload the char* cast to return the value of _Label.data()

    _Label.data() return a void* type.

    so what I did was

    Code:
    classname::operator char*(){   //this one cause the program to crash
    return (char*)(_Label.data());
    }

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    That's great, but that doesn't set the value of data, or use the resultant char *.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    I think my syntax is correct.
    The problem probably lie on what actually inside data ><.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    Quote Originally Posted by byebyebyezzz
    I think my syntax is correct.
    Your program compiled, so it is possible that the syntax is correct. A correct program has correct syntax, but not all programs with correct syntax are correct. So...
    Quote Originally Posted by Elysia
    Show the simplest possible compilable example that reproduces the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    I fixed the porblem. (the problem was with my team member's code)
    It's working fine now.
    Thanks all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advantages of c++ type casting over c type casting
    By kaibalya2008 in forum C++ Programming
    Replies: 10
    Last Post: 05-05-2009, 11:09 AM
  2. Replies: 3
    Last Post: 12-09-2008, 10:19 AM
  3. Replies: 2
    Last Post: 07-07-2008, 03:46 AM
  4. Replies: 1
    Last Post: 07-07-2008, 03:38 AM
  5. Casting operator behavior
    By pianorain in forum C++ Programming
    Replies: 9
    Last Post: 10-11-2004, 04:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21