CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2008
    Posts
    48

    Can we do operator overloading to structure's also ?

    Hi, I have some questios for which i need clarification.

    Is it possible to define operator overloading to structure type's also ?

    Is there any feature available in C++ which cannot done in C and can be done in C++ ?

    Thanks
    Kiran

  2. #2
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Can we do operator overloading to structure's also ?

    Quote Originally Posted by rsodimbakam
    Is it possible to define operator overloading to structure type's also ?
    Yes, a struct is like a class except for two small differences regarding defaults; In a struct variables and functions are public per default whereas in a class they're private. In a struct inheritance is public by default whereas in a class they're private.

    Stroustrup, the founder of C++, has said he wanted the struct to be included so C programmers would feel at home, but on the other hand he didn't want two different class concept. So in essence a struct is a class.

    You decide which one you want to use. I think the consensus usage is that you use a struct for simpler types, and a class for more complex. When operatator overloading is involved I think most would make it a class but it's up to you.

    Is there any feature available in C++ which cannot done in C and can be done in C++ ?
    Yes many. C++ is a superset of C. It starts with C and adds on mostly OO related stuff. It's sometimes said that C++ is C with classes. To this I would add templates which is a very important addition to C in C++.
    Last edited by _uj; September 22nd, 2008 at 12:50 AM.

  3. #3
    Join Date
    Sep 2008
    Posts
    48

    Re: Can we do operator overloading to structure's also ?

    Thanks for your clarification.

    I have read in some books that there is nothing available which we can do only in C++ and cannot be done in C(i mean everything we can do in C++ can be done in C also). What it means is that there are other ways exists in C for all OO related stuff.

    But how about C++ 'new' operator which we can overload it, for doing many memory management things. Is there any feature available in C which we can server the purpose of memory management ?

    Thanks.

  4. #4
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Can we do operator overloading to structure's also ?

    Quote Originally Posted by rsodimbakam
    I have read in some books that there is nothing available which we can do only in C++ and cannot be done in C(i mean everything we can do in C++ can be done in C also). What it means is that there are other ways exists in C for all OO related stuff.
    There's nothing we can do in C++ that cannot be done in Fortran too. If a language lacks feature X you can almost always simulate feature X using available language elements.
    But how about C++ 'new' operator which we can overload it, for doing many memory management things. Is there any feature available in C which we can server the purpose of memory management ?
    Sure there is. In C you have the malloc (and free) functions. You have them in C++ too because C is a subset of C++.

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Can we do operator overloading to structure's also ?

    It is trivially true that all useful languages are exactly equivalent in the sense that any program that can be written in one language can be written in another. To see why this is true, just consider that, at the time the program is run, the original language is irrelevant, since the computer is executing machine code.

    Where languages differ is in how easily one can express certain things. It's quite possible to simulate a polymorphic hierarchy in C - however (and I speak from bitter experience here), the resulting code is horrendous to try to understand, let alone maintain. It should also be possible to mimic polymorphism in FORTRAN - but I, for one, will run away very quickly if you ask me to try it.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Jan 2011
    Location
    INDIA
    Posts
    2

    Smile Re: Can we do operator overloading to structure's also ?

    Quote Originally Posted by rsodimbakam View Post
    Hi, I have some questios for which i need clarification.

    Is it possible to define operator overloading to structure type's also ?

    Is there any feature available in C++ which cannot done in C and can be done in C++ ?

    Thanks
    Kiran
    u can do everything wid structure as u do wid class definitely they hav got different type specifiers by default...try changing the keyword class wid struct in ur program.. try it

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Can we do operator overloading to structure's also ?

    Quote Originally Posted by _uj View Post
    Yes many. C++ is a superset of C.
    Almost. It is possible to write code that will compile as C, but not as C++.

    It's sometimes said that C++ is C with classes.
    People who say that usually don't write very good C++, but rather are writing C with a few language features cherry-picked from C++.

  8. #8
    Join Date
    May 2009
    Posts
    2,413

    Re: Can we do operator overloading to structure's also ?

    Quote Originally Posted by Lindley View Post
    Almost. It is possible to write code that will compile as C, but not as C++.
    Your argument is based on the misunderstanding that C++ is supposed to be a superset of C in a general sense. It is not and it cannot be because they're independent languages developing at their own pace. You cannot expect a program written in some version of C to compile with every version of C++.

    Instead C++ is a superset of C in a very specific sense, namely by standardisation. Every C++ standard defines exactly which C standard it's a superset of, including deviations. A program in that C must compile with that C++ or it's a flaw in the standard or the compiler.

    People who say that usually don't write very good C++, but rather are writing C with a few language features cherry-picked from C++.
    Do you really expect anyone to belive that you've made this observation? I don't for a second. In fact people who happen to refer to C++ as "C with classes" are much more likely to be hinting at the historical past of C++ than to be expressing their attitude towards C++ programming. I'd say chances are very high such people are excellent programmers.

    Yours is just another take on the pet theory that more is better. The more language features you manage to cram in and the more complex the ways you use them, the better the program must be. I don't share this view. Simplicity is a much better quality indicator. Simplicity in design and style. Too little abstraction is bad but too much is even worse.
    Last edited by nuzzle; January 17th, 2011 at 07:37 AM.

  9. #9
    Join Date
    Jan 2011
    Location
    Richmond, Va
    Posts
    32

    Re: Can we do operator overloading to structure's also ?

    Quote Originally Posted by Lindley View Post
    People who say that usually don't write very good C++, but rather are writing C with a few language features cherry-picked from C++.
    lol that may be true.

    I don't like all the extra c++ features because it's unnecessary most of the time, I'm sure alot of people feel the same.

    But If you're working on a huge enterprise level project, classes and all the other c++ features will be of great use. Otherwise, writing c in a procedural style is better (imo).

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Can we do operator overloading to structure's also ?

    Quote Originally Posted by Amaz1ng View Post
    lol that may be true.

    I don't like all the extra c++ features because it's unnecessary most of the time, I'm sure alot of people feel the same.

    But If you're working on a huge enterprise level project, classes and all the other c++ features will be of great use. Otherwise, writing c in a procedural style is better (imo).
    In most cases, the most important thing is that the produced code is robust and maintainable. To that end, there is great value in using std::vectors rather than malloc'd arrays, or using std::maps rather than complicated indexing schemes, and whatnot.

    But there's no need to go overboard. If a union will do, perhaps you don't have to break out a boost::variant. And if a simple procedural function will do, perhaps you don't need an object. It's always a judgment call.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured