LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 12-08-2009, 06:08 AM   #16
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454

This whole thread just proves/reminds that in reality three things are needed:

1) brains;
2) good scoping rules (e.g. "C", Perl, Pascal have them);
3) good preprocessor.

C++ originally was just a preprocessor.

My point is that OO featurer - if/when desired - should be implemented at source level.

Inheritance is not a value by itself - it's a means to achieve code extensibility, but just a means.
 
Old 12-08-2009, 06:52 AM   #17
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
So in Java if I don't say super() and the subclass has no parameter-less constructors, it calls a blank one?
 
Old 12-08-2009, 08:07 AM   #18
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by MTK358 View Post
So in Java if I don't say super() and the subclass has no parameter-less constructors, it calls a blank one?
That part of the language specification I linked tells you that if you don't start with a constructor call, the compiler will insert a call to a parameter-less constructor.

What happens if you call a parameter-less constructor that doesn't exist must be discussed elsewhere in the specification. But whatever that is, it happens the same regardless of whether you coded the call to the parameter-less constructor yourself or whether the compiler added it for you as described in 8.8.7

In C++ there are rules for whether a call to a parameter-less constructor that you haven't defined causes the compiler to generate an error message or whether it causes the compiler to invent a parameter-less constructor for you. As far as I recall, those rules are similar in Java.
 
Old 12-08-2009, 09:30 AM   #19
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I got this error compiling my C++ program. I don't understand why it says that I cannot call a base class's method.

Code:
$ cat Makefile
# Makefile for my widget toolkit

COMPILE = g++ -c -lX11 -I /usr/include/cairo -lcairo -Werror
LINK = g++ -lX11 -I /usr/include/cairo -lcairo -Werror -o main
OBJECTS = main.o Application.o Dimension.o TopLevelWindow.o Widget.o

main : $(OBJECTS)
        $(LINK) $(OBJECTS)

Application.o: src/Application.h src/Application.cpp
        $(COMPILE) src/Application.cpp

Dimension.o: src/Dimension.h src/Dimension.cpp
        $(COMPILE) src/Dimension.cpp

Widget.o: src/Widget.h src/Widget.cpp
        $(COMPILE) src/Widget.cpp

TopLevelWindow.o: src/TopLevelWindow.h src/TopLevelWindow.cpp
        $(COMPILE) src/TopLevelWindow.cpp

main.o: src/main.cpp
        $(COMPILE) src/main.cpp
$ make
g++ -c -lX11 -I /usr/include/cairo -lcairo -Werror src/main.cpp
src/main.cpp: In function ‘int main()’:
src/main.cpp:7: error: no matching function for call to ‘TopLevelWindow::TopLevelWindow(Application*, const char [10], Dimension)’
src/TopLevelWindow.h:16: note: candidates are: TopLevelWindow::TopLevelWindow(Application*, Dimension, std::string)
src/TopLevelWindow.h:12: note:                 TopLevelWindow::TopLevelWindow(const TopLevelWindow&)
src/Widget.h:27: error: ‘void Widget::show()’ is inaccessible
src/main.cpp:8: error: within this context
src/main.cpp:8: error: ‘Widget’ is not an accessible base of ‘TopLevelWindow’
make: *** [main.o] Error 1
 
Old 12-08-2009, 10:31 AM   #20
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
When you switch the second and third parameters, it tends not to be able to find the function.

The Makefile is much more complex than it needs to be - use make's implicit rules and GCC's dependency checking:
Code:
CXXFLAGS = -Wall -Wextra -Weffc++ -Werror `pkg-config x11 --cflags` `pkg-config cairo --cflags` -MD
LDFLAGS = `pkg-config x11 --libs` `pkg-config cairo --libs`
LINK.o = $(LINK.cpp)
.PHONY = all clean
SRCS = $(wildcard src/*.cpp)

all: main

main: $(SRCS:%.cpp=%.o)

clean:
	$(RM) -f main *.o *.d

-include *.d
 
1 members found this post helpful.
Old 12-08-2009, 10:44 AM   #21
MBybee
Member
 
Registered: Jan 2009
Location: wherever I can make a living
Distribution: OpenBSD / Debian / Ubuntu / Win7 / OpenVMS
Posts: 440

Rep: Reputation: 57
OO is entirely possible with C, BTW.
For example:
http://www.eventhelix.com/RealtimeMa...0Source%20Code
http://www.bolthole.com/OO-C-programming.html
http://bytes.com/topic/c/answers/530...lementing-oo-c

I often use C to do OO code.

If you're looking for a language that strongly encourages OO, maybe Ruby or Obj-C (as was suggested earlier). I wouldn't recommend Java, just because years of use have taught me that there is almost always a better way
 
Old 12-08-2009, 11:13 AM   #22
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by MTK358 View Post
I got this error compiling my C++ program. I don't understand why it says that I cannot call a base class's method.
It is pretty hard to tell you the error in your code when you didn't show us the code.

Despite that, tuxdev managed to tell you the cause of the error that is reported as line 7 of main.cpp

Looking at the messages, I can't be sure the error reported as line 8 is an unrelated error (as it appears) or a consequence of the line 7 error. Often it is best to fix errors as you understand them and then see if later errors go away or change.

If the second error remains, post the related code.
 
Old 12-08-2009, 12:19 PM   #23
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by tuxdev View Post
When you switch the second and third parameters, it tends not to be able to find the function.
What does that mean?

Quote:
Originally Posted by tuxdev View Post
The Makefile is much more complex than it needs to be - use make's implicit rules and GCC's dependency checking:
Code:
CXXFLAGS = -Wall -Wextra -Weffc++ -Werror `pkg-config x11 --cflags` `pkg-config cairo --cflags` -MD
LDFLAGS = `pkg-config x11 --libs` `pkg-config cairo --libs`
So as I understand pkg-config sets the right flags for the library and -Werror makes the compiler treat warnings as errors.

I still don't know what -Wall, -Wextra, Weffc++, and -MD mean.

Quote:
Originally Posted by tuxdev View Post
Code:
LINK.o = $(LINK.cpp)
.PHONY = all clean
SRCS = $(wildcard src/*.cpp)

all: main

main: $(SRCS:%.cpp=%.o)

clean:
    $(RM) -f main *.o *.d

-include *.d
I don't get this either. What are .d files, too?
 
Old 12-08-2009, 12:25 PM   #24
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by MBybee View Post
I knew about using structs instead of instance variables and functions that take the struct as a parameter instead of methods, and I like this technique, but I really need inheritance and polymorphism, too, and it is not obvious to me how you would do this as an extension of this technique.

But I wonder how i.e. GTK+ does it? it is written in pure C but still has some kind of object hierarchy.

Last edited by MTK358; 12-08-2009 at 12:27 PM.
 
Old 12-08-2009, 12:42 PM   #25
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
I still don't know what -Wall, -Wextra, Weffc++, and -MD mean.
-Wall and -Wextra makes gcc complain more about stuff you might be doing wrong. It has saved me from myself on many, many occasions.

-Weffc++ produces warnings based on some of what Scott Meyer wrote about in his "Effective C++" book series. Get a copy if you don't already have one and read through it.

I actually also use -ansi -pedantic so my C++ is as standard as possible, but that's not acceptable for some stuff.

-MD makes it produce .d dependency files which are then included into the Makefile for later builds. If you don't do some kind of automated dependency tree generation, it will bite you eventually.

"LINK.o = $(LINK.cpp)" tells make to link .o files together with the C++ linker (g++) rather than the default C linker (gcc).

".PHONY = all clean" tells make that the "all" and "clean" targets don't actually refer to files and should always be considered out-of-date.

"$(wildcard src/*.cpp)" returns a list of files that match the glob src/*.cpp

"main: $(SRCS:%.cpp=%.o)" tells make that the "main" file can be built from files in the SRCS variable with their extensions changed from .cpp to .o. make already knows how to make .o files from .cpp files.
 
Old 12-08-2009, 01:26 PM   #26
MBybee
Member
 
Registered: Jan 2009
Location: wherever I can make a living
Distribution: OpenBSD / Debian / Ubuntu / Win7 / OpenVMS
Posts: 440

Rep: Reputation: 57
Quote:
Originally Posted by MTK358 View Post
I knew about using structs instead of instance variables and functions that take the struct as a parameter instead of methods, and I like this technique, but I really need inheritance and polymorphism, too, and it is not obvious to me how you would do this as an extension of this technique.

But I wonder how i.e. GTK+ does it? it is written in pure C but still has some kind of object hierarchy.
There's really no point in debating it, but it is certainly possible, as demonstrated in this nice article. He comments that C is a non-OO language, but I think what he really means is that C doesn't force OO like some languages do.
http://www.embedded.com/97/fe29712.htm

In all fairness, OO is not a one size fits all approach to coding. There are, as always, many approaches to any problem. I usually consider that it comes down to either dev time or execution time (memory, etc). The determination is usually made by the company responsible - with F/OSS projects, it often tends to be determined by the project lead.
 
Old 12-08-2009, 03:15 PM   #27
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
An amusing rant against C++: C++ Frequently Questioned Answers.
 
Old 12-08-2009, 03:20 PM   #28
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I still see flaws with the pointer-to-superclass approach - What if you don't know the exact level of hierarchy a variable was created?
 
Old 12-08-2009, 03:31 PM   #29
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
I still see flaws with the pointer-to-superclass approach - What if you don't know the exact level of hierarchy a variable was created?
You're fundamentally assumed to know this. If you can't or don't want to keep track of it, of course the computer can help.. Hey, that's already been done and it's called "C++"!

The one thing about C++ using the same linker as C is that you *know* every feature in C++ can somehow be done in C, with various levels of pain.

Last edited by tuxdev; 12-08-2009 at 03:32 PM.
 
Old 12-08-2009, 03:59 PM   #30
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by tuxdev
Hey, that's already been done and it's called "C++"!
http://yosefk.com/c++fqa/defective.html

What's better about C:

See "Duplicate Facilities"

What's better about Java:

See "No high-level built-in types"
See "Manual memory management"

Last edited by MTK358; 12-08-2009 at 04:01 PM.
 
  


Reply



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
MailScanner alternatives cygnus-x1 Linux - Networking 1 11-28-2006 12:28 AM
trying to find alternatives acey99 Linux - Hardware 1 11-24-2006 10:03 AM
Vi alternatives Gins Linux - General 9 09-05-2005 10:29 AM
Alternatives to x? ampex189 Linux - Software 9 03-22-2005 01:21 PM
Alternatives matahchuah Linux - Software 1 02-17-2004 05:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:58 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration