Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learning C++ ^^
#1
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 : Intel i7-2600K
GPU : nVidia Geforce GTX 970
OS : Windows 8.1 x64
RAM : 16GB Corsair Vengance DDR3
PCSX2 : Constantly changing.
Reply

Sponsored links

#2
I googled this for you Tongue2
http://forums.sureshkumar.net/data-struc...ation.html
Reply
#3
(11-18-2009, 03:45 PM)rama Wrote: I googled this for you Tongue2
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 : Intel i7-2600K
GPU : nVidia Geforce GTX 970
OS : Windows 8.1 x64
RAM : 16GB Corsair Vengance DDR3
PCSX2 : Constantly changing.
Reply
#4
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 Dev Team
Reply
#5
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 : Intel i7-2600K
GPU : nVidia Geforce GTX 970
OS : Windows 8.1 x64
RAM : 16GB Corsair Vengance DDR3
PCSX2 : Constantly changing.
Reply
#6
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 Dev Team
Reply
#7
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.
Reply
#8
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 : Intel i7-2600K
GPU : nVidia Geforce GTX 970
OS : Windows 8.1 x64
RAM : 16GB Corsair Vengance DDR3
PCSX2 : Constantly changing.
Reply
#9
(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
Reply
#10
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 : Intel i7-2600K
GPU : nVidia Geforce GTX 970
OS : Windows 8.1 x64
RAM : 16GB Corsair Vengance DDR3
PCSX2 : Constantly changing.
Reply




Users browsing this thread: 1 Guest(s)