LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-01-2013, 08:13 AM   #16
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453

Quote:
Originally Posted by ta0kira View Post
Good point. In fact, you can even change the underlying language while keeping the API the same.

Kevin Barry
And, furthermore, a typical Linux application is dynamically linked, so, for example, parts of standard library, or even the whole standard library, can be replaced. A keyword to look up on 'net: LD_PRELOAD.
 
Old 07-02-2013, 06:59 PM   #17
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.0
Posts: 525
Blog Entries: 2

Rep: Reputation: 68
Quote:
Originally Posted by Sergei Steshenko View Post
To be fair, the same can be done in "C" - luckily, DLLs (I am not talking specifically about Windows) exist as concept and implementation.
It is true that you can always write a function that calls a library function then does its own thing. C++ does not add to the functionality of C.

What C++ does do is save some of the work that would otherwise go into modifying a module. If you wanted to "extend" a structure in C (ie add new members to it) you would first need to make a copy of the original structure to do so. Any function that took the original structure as a parameter would have to be re-written for the new structure even if it did nothing different. If both structures are present in the same program then you would need to give the new functions different names (there is no "method overloading" in C).

For a single programmer, this would mainly be just a copy and paste exercise and no great hardship. However, in a multi-programmer environment, there would be considerable scope for mis-communication. In C++ none of the foregoing is necessary. If you don't like the way a team mate has written a class, you can just make a derived class from it that is more to your liking. You can even make any instances of that derived class private to avoid making waves.

Last edited by psionl0; 07-02-2013 at 07:01 PM.
 
Old 07-02-2013, 07:16 PM   #18
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by psionl0 View Post
...Any function that took the original structure as a parameter would have to be re-written for the new structure even if it did nothing different. If both structures are present in the same program then you would need to give the new functions different names (there is no "method overloading" in C).

For a single programmer, this would mainly be just a copy and paste exercise and no great hardship. However, in a multi-programmer environment, there would be considerable scope for mis-communication. In C++ none of the foregoing is necessary. If you don't like the way a team mate has written a class, you can just make a derived class from it that is more to your liking. You can even make any instances of that derived class private to avoid making waves.
???????????????????????????

I define types in separate files and then include them into the code doing processing.

So, if I initially have in separate file (say, 'foo_struct.h') :

Code:
typedef struct
  {
  int i;
  double d;
  } foo_struct;
and then I decide to extend it (because I need new functionality implemented via new functions) to become

Code:
typedef struct
  {
  int i;
  double d;
  float f; // newly added field
  } foo_struct; // the 'foo_struct' name has _not_ changed
, what do I need to rewrite in already written functions ?

...

The horror and beauty of C++ is template engine, and C++ is overall more strictly typed language.
 
Old 07-02-2013, 07:23 PM   #19
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.0
Posts: 525
Blog Entries: 2

Rep: Reputation: 68
As a single programmer working on a single program, what you have done is fine. However, if a team mate was in charge of that section of code, he might not appreciate you modifying his header file.
 
Old 07-02-2013, 07:25 PM   #20
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by psionl0 View Post
As a single programmer working on a single program, what you have done is fine. However, if a team mate was in charge of that section of code, he might not appreciate you modifying his header file.
Alas, adopted code screwing practices are mostly nonsense. Header files are interfaces and as such should be controlled by project level architects and not "everyday" coders.

Conceptually header files are derived from spec.
 
Old 07-02-2013, 07:25 PM   #21
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Sergei Steshenko View Post
???????????????????????????

I define types in separate files and then include them into the code doing processing.

So, if I initially have in separate file (say, 'foo_struct.h') :

Code:
typedef struct
  {
  int i;
  double d;
  } foo_struct;
and then I decide to extend it (because I need new functionality implemented via new functions) to become

Code:
typedef struct
  {
  int i;
  double d;
  float f; // newly added field
  } foo_struct; // the 'foo_struct' name has _not_ changed
, what do I need to rewrite in already written functions ?

...

The horror and beauty of C++ is template engine, and C++ is overall more strictly typed language.
Unfortunately it's not that simple if data structures, or pointers to them, are passed among libraries that are compiled at different times. In general, you need to recompile every library or binary that accesses a member of a class/struct if you change it. The few exceptions are in C++ (e.g. only changing the body of a function, adding a non-virtual member function,) and only if you link all member functions into a single library and link users of the class to that library. This generally rules out trying to tweak templates without anyone noticing.

Kevin Barry

Last edited by ta0kira; 07-02-2013 at 07:28 PM.
 
Old 07-02-2013, 07:38 PM   #22
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.0
Posts: 525
Blog Entries: 2

Rep: Reputation: 68
Quote:
Originally Posted by Sergei Steshenko View Post
Header files are interfaces and as such should be controlled by project level architects and not "everyday" coders.
If a bureaucracy is involved then that makes what you suggested even more difficult.

Quote:
Originally Posted by ta0kira View Post
In general, you need to recompile every library or binary that accesses a member of a class/struct if you change it.
I take it you are referring to C. In C++ creating an extended class doesn't involve modifying the original class.
EDIT: I see that you have clarified your post.

Last edited by psionl0; 07-02-2013 at 07:45 PM.
 
Old 07-02-2013, 07:49 PM   #23
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by psionl0 View Post
If a bureaucracy is involved then that makes what you suggested even more difficult.

I take it you are referring to C. In C++ creating an extended class doesn't involve modifying the original class.
What bureaucracy ?

For example, I used to be VLSI project integrator, and part of my job duties was to maintain the project level header files.

Designers did not change project level header files, I did not change block level header file.

...

It is possible to have per developer extensions in "C", with some textual overhead, e.g. to begin with

Code:
// contents of 'foo_body' include file
  int i;
  double d;

Code:
// contents of 'foo_struct.h'  include file
typedef struct
  {
#include "foo_body"
  } foo_struct
.

Then "derived class" can be implemented as

Code:
// contents of 'foo_struct.h'  include file
#define foo_struct extended_foo_struct
typedef struct
  {
#include "foo_body"
#include "foo_extension_body"
  } foo_struct
, and each developer chooses actual name and contents for/of 'extended_foo_struct', 'foo_extension_body'.
 
Old 07-02-2013, 08:09 PM   #24
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.0
Posts: 525
Blog Entries: 2

Rep: Reputation: 68
OK, so not impossible but it has definitely become more complicated than you originally made out.

Is there any reason why you would prefer your programmers to use C instead of C++ in the scenario you have outlined?
 
Old 07-02-2013, 08:48 PM   #25
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by psionl0 View Post
...
Is there any reason why you would prefer your programmers to use C instead of C++ in the scenario you have outlined?
I didn't say I would prefer your programmers to use C instead of C++, I was simply pointing out that perceived/declared in this thread advantages of C++ are not that advantages - because the examples are implementable in "C".

I reiterate: the two real IMO advantages of C++ is templates (with all their debugability horror) and overall better type strictness.

IIRC, in 2011 stndard threads are part of C++ standard library, there is some lambda-calculus, better enums, etc.
 
Old 07-02-2013, 10:12 PM   #26
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.0
Posts: 525
Blog Entries: 2

Rep: Reputation: 68
Quote:
Originally Posted by Sergei Steshenko View Post
I reiterate: the two real IMO advantages of C++ is templates (with all their debugability horror) ...
I don't understand what you are saying. You appear to be dismissing the ability to create a derived class without the need to touch the original source code as having no particular advantage.
 
Old 07-03-2013, 07:36 AM   #27
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by psionl0 View Post
I don't understand what you are saying. You appear to be dismissing the ability to create a derived class without the need to touch the original source code as having no particular advantage.
I have shown that properly textually structured "C" code allows to create derived class (i.e. derived, i.e. extended struct) without the need to touch "the original source code", i.e. the original struct.
 
Old 07-03-2013, 08:14 PM   #28
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.0
Posts: 525
Blog Entries: 2

Rep: Reputation: 68
Quote:
Originally Posted by Sergei Steshenko View Post
I have shown that properly textually structured "C" code allows to create derived class (i.e. derived, i.e. extended struct) without the need to touch "the original source code", i.e. the original struct.
That was the bit I understood. You have demonstrated that it is possible to set up "extendable" structures in C if you have the necessary "textual overhead" in the header files.

If the other person hasn't gone to that trouble then you are back to copy and paste. Also, in either case, you can not use the other person's object file - you have to re-compile his source in order for the functions to use the extended structure.

Method overriding is not possible in C.
Code:
class A
{
   ... ... ...
   public:
      void a_method()
      {
         ... ... ...
      }
};

class B : public A
{
   ... ... ...
   public:
      void a_method()
      {
         ... ... ...
         A::a_method();
         ... ... ...
      }
};
 
Old 07-03-2013, 08:36 PM   #29
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by psionl0 View Post
...
Method overriding is not possible in C.
...
Method overriding is possible in "C" since "C" has pointers to functions.

What is impossible in "C" is to create a number of functions with the same name, but different signatures, and let the compiler choose the needed one according to number of arguments and their type.
 
Old 07-03-2013, 09:10 PM   #30
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.0
Posts: 525
Blog Entries: 2

Rep: Reputation: 68
Quote:
Originally Posted by Sergei Steshenko View Post
Method overriding is possible in "C" since "C" has pointers to functions.
This is true.

If keeping the same names is not a matter of life and death, you can extend a struct and its "methods" in C without recompiling the original source in the following way:
Code:
struct base_struct
{
   ... ... ...
};

struct extended_struct
{
   // copied and pasted from base_struct
   ... ... ...  // extra bits
};

void base_func(struct base_struct b)
{
   ... ... ...
};

void copy_extended(struct base_struct *b; struct extended_struct e)
{
   // copies members from extended struct to base struct
};

void extended_func(struct extended_struct e)
{
   base_struct b;
   ... ... ...
   copy_extended(&b, e);
   base_func(b);
   ... ... ...
}

Last edited by psionl0; 07-03-2013 at 09:14 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does making a class a 'friend' make the class' member classes 'friends'? Jusctsch Programming 7 11-17-2011 07:58 PM
C++ - Using class pointers to use class functions? golmschenk Programming 2 04-24-2011 12:41 AM
LXer: OpenOffice.org Calc functions, part 1: Understanding functions LXer Syndicated Linux News 0 03-31-2007 12:01 PM
PHP: Calling class functions from another class. unreal128 Programming 5 11-24-2006 03:04 AM
C++ - problem with pointers to class member functions Nylex Programming 3 01-15-2006 10:14 AM


All times are GMT -5. The time now is 07:14 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration