use C to implement all C++ features?

This is a discussion on use C to implement all C++ features? within the C++ Programming forums, part of the General Programming Boards category; Somebody claimed C++ is not necessary because he can use C to implement all C++ features. What do you think?...

  1. #1
    CALVIN
    Guest

    use C to implement all C++ features?

    Somebody claimed C++ is not necessary because he can use C to
    implement all C++ features. What do you think?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    of course that's true for some features of C++, but C++ has those features already and they're much easier to use this way.

    in C, a virtual function table is a struct full of function pointers.

    but you would be hard pressed to pull off templates with some kind of strange macro scenario. certainly inline functions closely resemble macros.

    in short, tell your friend to STFU.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I don't need C either, I just use a hexeditor, creating the exes myself. That way they become much smaller and I don't have to compile any code.
    Everything can be done with a hexeditor, there's no need to learn C.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    CALVIN
    Guest
    How would you implement a function which takes a reference as
    the argument in C? For example,

    Code:
    void foo(SomeType&)

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Perhaps like this:
    Code:
    void __foo(SomeType* __tempname)
    {
      #define var (*__tempname)
      //.... 
      #undef var
    }
    
    #define foo(a) __foo(&(a))
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >Somebody claimed C++ is not necessary because he can use C to
    >implement all C++ features. What do you think?

    Or he doesn't know C++ or he is just stupid. Anyway, I'm very curious how he would implemented something like operator-overloading in C.

  7. #7
    Terrence
    Guest
    If this was the case, they'd implement the stl for c as well.

    Any program you can make in c++, can be made in c, maybe that's what he means.

  8. #8
    CALVIN
    Guest
    Sang-drax,

    You are passing a pointer to SomeType as argument. You have to
    de-referece the pointer in order to change the content of the passed in argument in the function. That's different. For example,

    Code:
    void foo(int& v)
    {
      v++;
    }
    
    int main()
    {
      int a = 3;
      
      foo(a);
      cout << a;
    }

  9. #9
    CALVIN
    Guest

    operator overloading

    Shiro,

    Member operator is nothing new but another form of member function. You could easily use a function in C to replace the operator.

  10. #10
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by CALVIN
    Sang-drax,

    You are passing a pointer to SomeType as argument. You have to
    de-referece the pointer in order to change the content of the passed in argument in the function. That's different. For example,

    What? Have you tried my example?

    Code:
    void __foo(int* __tempname)
    {
      #define var (*__tempname)
      var++;
      #undef var
    }
    #define foo(a) __foo(&(a))
    
    int main()
    {
      int a = 3;
      foo(a);
    
      printf("%d",a);
      return 0;
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #11
    CALVIN
    Guest

    reference & and pointer

    var++ => (*__tempname)++. You're dereferencing the passed-in
    pointer.

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    Somebody claimed C++ is not necessary because he can use C to
    implement all C++ features. What do you think?
    They could write a C++ interpreter/compiler in C, but apart from that why the would they want to? If you want to use a C++ feature use C++. Ask the person how they would implement something as simple a member functions in straight C. A C++ compiler resolves some of the C++ features at compile time. If you start implementing them using C you'll have to be careful or you start imposing runtime overhead, or not providing all the features that would be present in C++.

    If they mean then they can do anything in C that you can in C++, then this is true, but that's not implementing C++ in C (unless you're writing a compiler/interpreter). You may be able to replace operator overloading with functions, but then you're not really implementing a C++ feature, are you? Rather using an alternative technique.
    Joe

  13. #13
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: reference & and pointer

    Originally posted by CALVIN
    var++ => (*__tempname)++. You're dereferencing the passed-in
    pointer.
    And how do you think it's done internally using references?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  14. #14

  15. #15
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Sang- drax is right......the code produced for pointers is the same as for references....

    The only difference is in the syntax of your code

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-20-2007, 06:55 PM
  2. Implement "Whats This" using Win32
    By hemanth.balaji in forum Windows Programming
    Replies: 1
    Last Post: 05-29-2005, 06:03 AM
  3. use C to implement all C++ features?
    By Inquirer in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2003, 01:41 PM
  4. Implement malloc()
    By rahuls in forum C Programming
    Replies: 5
    Last Post: 03-20-2003, 07:50 PM
  5. Can't Implement Tabbing
    By Invincible in forum Windows Programming
    Replies: 10
    Last Post: 02-27-2002, 04:09 AM

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