..:: PCSX2 Forums ::..
Register | Help | Log In |
Register PCSX2 Site Forums Member List Donate View Today's Posts Search

Current time: 03-21-2010, 10:52 PM Hello There, Guest! (Login — Register)

..:: PCSX2 Forums ::.. / Off Topic / Chatterbox / Learning C++ ^^

1 user browsing this thread: (0 members, and 1 guest).

Pages (3): 1 2 3 Next »
Post Reply 
 
Thread Rating:
  • 2 Votes - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threaded Mode | Linear Mode
Learning C++ ^^
Author Message
AtlasRedux Offline
Junior Member
*

Posts: 41
Joined: Nov 2009
Location:
Post: #1
Learning C++ ^^
OK, I'm a nublet on C++ ( started to read up on it yesterday after 10 years without touching anything except Basic, heh ), but got a mind for logic and easily grasp the concept of OOP.
Now, what I do NOT grasp and which is not really explained ( yes, I've googled Tongue ) ... why the heck even use declarations?
Isn't it more efficient to just do a definition at once instead of first declaring and THEN define?
I do get that a decl. doesn't set space for a func. and hence ... it really doesn't do anything except perhaps help the linker or something. It just breaks up the code afaik, considering the ODR ...
I assume there's some huge "AHAAA" thing here that of some reason isn't really well explained in any teachings online Wink

CPU : IC2D E8200, FSB @ 1500MHz, real CPU : 3GHz
GPU : nVidia Geforce 8800GT ( stock settings )
OS : Win7 x64
4GB of crappy RAM.
PCSX2 : R1888 of full FPS mayhem.
CPU Passmark : 2333.7
(This post was last modified: 11-19-2009 06:20 AM by AtlasRedux. Edit Reason: N/A)
11-18-2009 01:51 PM
Find all posts by this user Quote this message in a reply
rama Offline
Cycle Counter
******

Posts: 1.794
Joined: Nov 2008
Location:
Post: #2
RE: Declaration VS Definition
I googled this for you Tongue
http://forums.sureshkumar.net/data-struc...ation.html
11-18-2009 03:45 PM
Find all posts by this user Quote this message in a reply
AtlasRedux Offline
Junior Member
*

Posts: 41
Joined: Nov 2009
Location:
Post: #3
RE: Declaration VS Definition
(11-18-2009 03:45 PM)rama Wrote:  I googled this for you Tongue
http://forums.sureshkumar.net/data-struc...ation.html

No, because that didn't answer my question Wink I know the difference, I just don't see why we even need to use Decl. when a Def. does a Decl. automatically.
So no, you will NOT get me caught in my own Google That xD
To shorten my question : what the heck we need Decl. when Def. does it for us!
Specially when a declaration pretty much is useless without a Def. later on, but again, that is as far as I know.
Just trying to learn here Wink

CPU : IC2D E8200, FSB @ 1500MHz, real CPU : 3GHz
GPU : nVidia Geforce 8800GT ( stock settings )
OS : Win7 x64
4GB of crappy RAM.
PCSX2 : R1888 of full FPS mayhem.
CPU Passmark : 2333.7
(This post was last modified: 11-18-2009 04:53 PM by AtlasRedux. Edit Reason: )
11-18-2009 04:48 PM
Find all posts by this user Quote this message in a reply
Air Offline
PCSX2 Programmer
******

Posts: 1.144
Joined: Nov 2008
Location: Meshoppen, PA
Post: #4
RE: Declaration VS Definition
First off:

I personally hate the ambiguity of the two words being way too similar. In my own code I use the terms Implementation and Declaration. Besides the fact that 'implementation' just makes more sense than 'definition', it's a lot harder to go dyslexic while reading a terse tech article and get the two confused.

Second, to answer the question:

C++ is not a true multi-pass parser. It cannot resolve ambiguities ahead of time. Example:

Code:
struct OtherType
{
    void TraceLog( const Cee& src )
    {
        printf( "Something: %d", Cee.GetValue() );
    }
};

struct Cee
{
    void DoSomething( const OtherType& src )
    {
        src.TraceLog( *this );
    }

    int GetValue() const
    {
        return m_member;
    }
};

The compilation fails because the Implementation of OtherType::TraceLog can't see the definition of type Cee. So in order to get around the pathetic 1980's style limitations of the C++ parser, we have to do this:

Code:
// Forward declarations allow us to declare the class prototypes that are inter-dependent:
struct Cee;
struct OtherType;

struct OtherType
{
    void TraceLog( const Cee& src );
};

struct Cee
{
    void DoSomething( const OtherType& src );
    int GetValue() const;
};

void OtherType::TraceLog( const Cee& src )
{
    printf( "Something: %d", Cee.GetValue() );
}

void Cee::DoSomething( const OtherType& src )
{
    src.TraceLog( *this );
}

int Cee::GetValue() const
{
    return m_member;
}

The second reason to separate code implementations and declarations, even when these inter-dependent classes don't exist, is because the C++ parser is really slow. Modern C++ standards syntax can require tremendous amounts of CPU time to parse, and if you have a project with hundreds of source and header files, and you were to include most or all code possible into headers, it can increase compilation time exponentially as C++ must re-parse all implementations on every file and then resolve redundant definitions at link-time.

Jake Stine (Air) - Programmer - Pcsx2 Development Team
(This post was last modified: 11-18-2009 05:16 PM by Air. Edit Reason: )
11-18-2009 05:14 PM
Find all posts by this user Quote this message in a reply
AtlasRedux Offline
Junior Member
*

Posts: 41
Joined: Nov 2009
Location:
Post: #5
RE: Declaration VS Definition
Aaah, gotcha. Didn't think like that and wasn't very well explained in the tutorials I've read so far, I had my mind set to multi-passes. They just say "here's how it work, bloody use it like we say and don't think too much."
That REALLY defies the way of OOP ( when you're not the client that is Wink ) and I need to know how stuff works when I learn it xD
"Here's a car, it moves."
"How?"
"Don't think about it, it just does."
"Tell me, or die."
Both learnings duly noted into brain for further use, thx a lot Smile

EDIT : I actually did get myself on the right track then, since I said "to tell the linker or something" .... but I thought it was "true" multipass and had my head stuck on that, so this helped a lot.

CPU : IC2D E8200, FSB @ 1500MHz, real CPU : 3GHz
GPU : nVidia Geforce 8800GT ( stock settings )
OS : Win7 x64
4GB of crappy RAM.
PCSX2 : R1888 of full FPS mayhem.
CPU Passmark : 2333.7
(This post was last modified: 11-18-2009 06:05 PM by AtlasRedux. Edit Reason: )
11-18-2009 05:51 PM
Find all posts by this user Quote this message in a reply
Air Offline
PCSX2 Programmer
******

Posts: 1.144
Joined: Nov 2008
Location: Meshoppen, PA
Post: #6
RE: Declaration VS Definition
Yeah only time C++ is multipass is when parsing the contents of a class/struct. It'll parse all declarations in the class first, so that a class can reference its own members in any order, without dependencies. And then it goes back and compiles any code that's implemented inline to the class declaration.

But as soon as you leave class scope, it goes back to single-pass style.

Jake Stine (Air) - Programmer - Pcsx2 Development Team
11-18-2009 07:16 PM
Find all posts by this user Quote this message in a reply
Zeydlitz Offline
Plugin Author
*****

Posts: 961
Joined: Dec 2008
Location:
Post: #7
RE: Declaration VS Definition
Well, it's not just true: there C-compiller have several stages (passes) at least preprocessor -- pre-compiller -- optimizer -- compiller (and sometimes a few passes more). But form years of Algol-60 compiller's are thinking set of as turing machine's, so at every pass it prefer to not know about code bellow current position.
11-19-2009 02:43 AM
Find all posts by this user Quote this message in a reply
AtlasRedux Offline
Junior Member
*

Posts: 41
Joined: Nov 2009
Location:
Post: #8
Learning C++ ^^
Yup, get the point then Smile
C++ seems fun to learn, I've decided to try and fail before I read the example and detailed explanations, which ended up in actually being able to manipulate a textfile correctly with ofstream and ifstream using what logically should be used just like the example wanted ^^ Made me feel happy happy and makes me remember it better.
Now, time to try that with boot.ini!
Lemme see, a system call "attrib C:\boot.ini -r -h" ....... nah xD
I predict years of frustration learning this. And fun.
EDIT: Stupid "cin" not putting spaces in strings, hooray for getline(cin ... is there a workaround for that, or just gotta live with getline everything?
You guys are hereby promoted to be my "answer when the heck you want"-teachers xD

CPU : IC2D E8200, FSB @ 1500MHz, real CPU : 3GHz
GPU : nVidia Geforce 8800GT ( stock settings )
OS : Win7 x64
4GB of crappy RAM.
PCSX2 : R1888 of full FPS mayhem.
CPU Passmark : 2333.7
(This post was last modified: 11-19-2009 06:20 AM by AtlasRedux. Edit Reason: Topic apparantly changed a bit)
11-19-2009 05:50 AM
Find all posts by this user Quote this message in a reply
echosierra Offline
PCSX2 dev wannabe
******

Posts: 588
Joined: Dec 2008
Location:
Post: #9
RE: Learning C++ ^^
(11-19-2009 05:50 AM)AtlasRedux Wrote:  Yup, get the point then Smile
C++ seems fun to learn, I've decided to try and fail before I read the example and detailed explanations, which ended up in actually being able to manipulate a textfile correctly with ofstream and ifstream using what logically should be used just like the example wanted ^^ Made me feel happy happy and makes me remember it better.
Now, time to try that with boot.ini!
Lemme see, a system call "attrib C:\boot.ini -r -h" ....... nah xD
I predict years of frustration learning this. And fun.
EDIT: Stupid "cin" not putting spaces in strings, hooray for getline(cin ... is there a workaround for that, or just gotta live with getline everything?
You guys are hereby promoted to be my "answer when the heck you want"-teachers xD

Learn to love this site, when learning it's the best definitive reference there is.

Do you mean it isn't returning trailing spaces? ie. if you type "This is a test. " it only returns "This is a test." ? If you can narrow it down to a small code fragment, post it within [code] tags; it's a whole lot easier than attempting to describe something like this lol.

I never did like using cin and cout, I had a lot of problems dealing with string input in a clear and predicable way. Switching to getline is worth doing if only to get experience in input validation, give it a try and see which suits you better.

"This thread should be closed immediately, it causes parallel imagination and multiprocess hallucination" --ardhi
11-19-2009 04:01 PM
Find all posts by this user Quote this message in a reply
AtlasRedux Offline
Junior Member
*

Posts: 41
Joined: Nov 2009
Location:
Post: #10
RE: Learning C++ ^^
It's more fun here xD It's off-topic and I'm only using this 1 thread, so it's allowed Wink and people here have more experienced, so they can give tips in the looooong future about solutions that isn't mentioned in the "standard" tutorials or nublets on other sites ( like the question I opened the thread with )
Anyways, example code ( ignore the includes, I just have them there since I use the same file for all testings ) :

Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

int main() {
    string crap;
    cout << "Type in anything containing spaces : ";
    cin >> crap;
    cout << crap ;
}

In example above, typing in "This really sucks" will cause cin to only store "This" in the crap.

So gotta switch out the ' cin >> crap; ' with ' getline(cin, crap); '
What I am curious about is if it's possible to get cin to contain spaces using the >>

EDIT : I'm using Bruce Eckel's "Thinking in C++" btw

CPU : IC2D E8200, FSB @ 1500MHz, real CPU : 3GHz
GPU : nVidia Geforce 8800GT ( stock settings )
OS : Win7 x64
4GB of crappy RAM.
PCSX2 : R1888 of full FPS mayhem.
CPU Passmark : 2333.7
(This post was last modified: 11-19-2009 07:19 PM by AtlasRedux. Edit Reason: )
11-19-2009 04:18 PM
Find all posts by this user Quote this message in a reply
« Next Oldest | Next Newest »
Pages (3): 1 2 3 Next »
Post Reply 


  • View a Printable Version
  • Send this Thread to a Friend
  • Subscribe to this thread
Forum Jump:


Current time: 03-21-2010, 10:52 PM

Contact Us | PCSX2 | Return to Top | Return to Content | Lite (Archive) Mode | RSS Syndication

Powered By MyBB, © 2002-2010 MyBB Group.
Theme created by IncadudeF and modified by bositman