Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 25th, 2006, 12:01 AM   #1
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
class/constructor

I have to accomplish this weird task, no idea how to go about it.

there is a template class named "Scanner", and its constructor is used to open a file.

and there is a class named "Parser", and it's supposed to take the filename from main.cpp and pass it to the class Scanner. Oh also, all the member functions of the Parser class needs to have access to the declaraton involving scanner within the parser class.

I am more confused with the syntax than anything

Trial I have had:

I tried using Scanner <string> Si; inside the public section of the Parser class, then tried updating the "Si" in the Parser constructor so that Si now has the filename, it didnt work :
Parser (string s)
{
Scanner <string> Si(s);
NT = Si.cget();
GlobalRootM = prog();

}
^ string s would be the filename passed to the Parser class by main.cpp, and prog() is a parser member function and it'll call other parser functions, and each of these functions will rely on the Scanner class for the next token. (Typical recursive descent parser!)
I'm obviously doing something wrong with the syntax, but I dont know what!

Oh, dont ask me why i am linking parser and scanner this way to get the filename (I have to!! lol)
programmingnoob is offline   Reply With Quote
Old Aug 25th, 2006, 12:10 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
If I'm understanding you right, your class declaration looks something like this;
class Parser
{
     public:
          Parser(const std::string &);
     private:
          Scanner<std::string> Si;
};
where your Scanner<std::string> class has a constructor that accepts a std::string.

Assuming this is correct, then one way of implementing your constructor is outside your class declaration;
Parser::Parser(const std::string &s) : Si(s)
{
    NT = Si.get();
     // etc
}

A couple of additional notes;

1) If you have used "using namespace std;" within your class declaration (or in the header file that contains it), good practice says "don't do that".

2) If prog() is a virtual member function of class Parser, or if it calls virtual member functions, then it is usually a good idea to NOT call it from a constructor of class Parser.
grumpy is offline   Reply With Quote
Old Aug 25th, 2006, 12:29 AM   #3
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Virtual functions overridden in a subclass do not get called from the constructor of the base class, only the base class versions get called. This is why its a good idea not to do that.
Game_Ender is offline   Reply With Quote
Old Aug 31st, 2006, 11:18 AM   #4
magnus.therning
Programmer
 
magnus.therning's Avatar
 
Join Date: May 2006
Location: Cambridge, UK
Posts: 85
Rep Power: 3 magnus.therning is on a distinguished road
AFAIK that is because the state of the subclass instance is somewhat up in the air while the baseclass constructor is executed.

(I couldn't help butting in :-)
__________________
Don't comment bad code - rewrite it.
- The Elements of Programming Style (Kernighan & Plaugher)
magnus.therning is offline   Reply With Quote
Old Aug 31st, 2006, 6:31 PM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by magnus.therning
AFAIK that is because the state of the subclass instance is somewhat up in the air while the baseclass constructor is executed.
I suppose that's one way of putting it. The reason is order of construction. The process of constructing an object of a derived class constructs the base class sub-objects first, and then the parts specific to the derived class. The final resolution of virtual function is among the parts specific to the derived class. This means, the base class constructor has access to no information about the fact a derived class object is being created.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:52 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC