Math Is Fun Forum
  Discussion about math, puzzles, games and fun.   Useful symbols: √ ∞ ≠ ≤ ≥ ≈ ⇒ ∈ Δ θ ∴ ∑ ∫ π -

Login

Username

Password

Not registered yet?

Post a reply

Go back

Write your message and submit
:) :| :( :D :o ;) :/ :P :lol: :mad: :rolleyes: :cool: | :dizzy :eek :kiss :roflol :rolleyes :shame :down :up :touched :sleep :wave :swear :tongue :what :faint :dunno
Options

Go back

Topic review (newest first)

mikau
2008-02-15 02:05:11

Yeah, but in first.cpp it declares 'external int' without the 'extern' keyword. But, because of its name, I pressume it also is external.

So does the 'extern int externalInt' declaration in second.ccp create a new variable or does it refer to the same thing as the external int in first.cpp?

luca-deltodesco
2008-02-14 20:08:41

extern makes available the variable accross all cpp files.

although ive never seen it used in that sense, where its declared once in the primary file, and then declared external in another, it seems rather messy hmm

mikau
2008-02-14 18:47:10

Todays subject: internal and external linkage!

I'm just reading about namespaces in my C++ book for the first time. It began by introducing the subject of linkage, but its description is rather unclear.

here's my books description:

Linkage
Names can have internal and external linkage. These two terms refer to the use or availability of a name across multiple translation units or within a single translation unit. Any name having internal linkage can only be referred to within the translation unit in which it is defined. Names having external linkage are available to other translation units.

okay, but they don't explain what a 'translation unit' is. Immediatley after it uses the following listings to demonstrate internal and external linkage

first.cpp

Code:

int externalInt = 5;
const int j = 10;
int main()
{
  return 0;
}

second.cpp

Code:

extern int externalInt;
int anExternalInt = 10;
const int j = 10;

after this it says the externalInt variable defined in line 1 of first.cpp has external linkage.

I don't follow. First, this is the first time i have ever seen the 'extern' keyword. So i'm not sure what the first line in second.cpp means, and how many instances of externalInt there are.

Usually my book explains things better than this.  Can anyone explain to me whats happening between these two .cpp files?

mikau
2008-02-08 12:32:29

thanks!

Btw, i had two queestions. Can you make a pointer to a method of class A point to a method of class B where B is a subclass of A? It failed to work when I tried it but I was wondering if maybe you could do it with casting.

Also, can you return a pointer to a function from a function? that is, can a functions return type be a pointer to a function?

Ricky
2008-02-08 11:23:24

& is proper, because you want that function's address.

pointers to functions anyway as it seems to be a discouraged technique

Not really, however it is fairly rare when they are used.  See std::sort for a proper example.

the license is apparently about 17 years old...

Yes, however this does not mean that the compiler is.  A license appears from the start date.  So if I start a website in 2000 and keep it running till 2008, I would still put a copyright of 2000 on it.

mikau
2008-02-08 09:09:58

this is weird...

I clicked on help/about dev-C++ in the IDE, here's what it says at the top:

Code:

GNU GENERAL PUBLIC LICENSE

                       Version 2, June 1991

 Copyright (C) 1989, 1991 Free Software Foundation, Inc.
                          675 Mass Ave, Cambridge, MA 02139, USA
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.

the license is apparently about 17 years old... but I seriously doubt they would put a 1991 compiler on a website still in use today. (or at least as of august 2007)

It may be just an outdated compiler, but even if it was, it still understands the declaration:
int (Dog*p)(long, double);
it just doesn't let me do anything with p after that. At least i haven't found a way yet.

anyway, Ricky. This isn't a major issue, first because i normally use my borland compiler, and second because i don't really intend to use pointers to functions anyway as it seems to be a discouraged technique. The point is that I know the correct way to do it when I need it, and to recognize when other people use it.

SO! which is the correct method? To use p = dog::bark; or p = &dog::bark; ?

Ricky
2008-02-08 04:59:50

Bloodshed Dev-C++ is a full-featured Integrated Development Environment (IDE) for the C/C++ programming language. It uses Mingw port of GCC (GNU Compiler Collection) as it's compiler. Dev-C++ can also be used in combination with Cygwin or any other GCC based compiler.

If you don't know what this means here is a bit of an explanation.  The compiler is a program which takes input files (and some addition parameters) and spits out some type of byte-code.  Compilers, like any programs, must be run in some form.  As with any program, it must be run.  Since the compiler typically requires input (namely, code) it must be run from the command line.

That's all fine and dandy, but compilers being run from the command line can be typically hard to use.  It is tedious to type in a line every time you want to compile.  Also, there are many options which can make that line very long and confusing.  In order to simplify this, a Integrated Developement Environment is used.  All an IDE is, is a program which runs a compiler.  As such, they are almost always not connected, but rather the IDE just generates that compile line for you.  It would be rather idiotic for an IDE to not come with a text editor to allow you to modify your code, and some even have debugging tools.

So in the above quote, Bloodshed is just an IDE.  It runs off the Mingw port of GCC.  GCC, in turn, is the standard compiler used by Linux systems.  I'd have to do some testing with Mingw, but it looks like if that was your code, then there is a pretty darn big bug in the compiler.

mikau
2008-02-08 04:18:40

they don't have C++ compilers on my schools computers (my school is java biased!)   

here's where i download it
http://www.bloodshed.net/devcpp.html

Ricky
2008-02-08 01:11:59

What compilers are you using?  Need name and version numbers.

mikau
2008-02-08 00:55:19

mm... i think you read my post too fast, Ricky! I said I tried that and it didn't work.

But at the moment I'm about to leave for school and haven't much time so.. perhaps I read YOUR post too fast. I'll look more carefully later.

Ricky
2008-02-08 00:19:21

It's just a typo I believe.

Code:

#include <iostream>

class Dog
{
  public:
  Dog() {}
  ~Dog() {}
  
  // method to be pointed to
  int bark(long l, double d) { std::cout << "bark " << (l*d) << " times\n"; return 0; } 
};

int main()
{
  // declare pointer to any dog method that takes a long and a double and returns an int
  int (Dog::*p)(long, double); 
  p = &Dog::bark; // bad???
  Dog pokey;
  (pokey.*p)(2,4);
  return 0;
}
mikau
2008-02-07 16:20:33

my latest topic: Pointers to functions!

I'm having some compiler/book disagreement when using pointers to member functions.

consider the following program

Code:

#include <iostream>

class Dog
{
  public:
  Dog() {}
  ~Dog() {}
  
  // method to be pointed to
  int bark(long l, double d) { std::cout << "bark " << (l*d) << " times\n"; return 0; } 
};

int main()
{
  // declare pointer to any dog method that takes a long and a double and returns an int
  int (Dog::*p)(long, double); 
  p = Dog::bark; // bad???
  Dog pokey;
  (pokey.*p)(2,4);
  return 0;
}

according to my book, the statement p = Dog::bark; is how you make p point to that method. On my compiler at home (borland) it compiles and works fine. But today I was using a compiler on a computer in my schools computer lab, and it complained about that statement as being an invalid use of a nonstatic method.

I googled some articles. Some of which said you have to use 'p = &Dog::bark;' but the compiler issued the same complaint.

Note the pointer declaration worked fine, but the compiler yelled everytime i tried to assign it to something. But again, both 'p = Dog::bark;' and "p = &Dog::bark;' compiled and ran on my home compiler.

So my question is, is the compiler at school behaving improperly, or is my current compiler outdated? IF so, what is the correct way to make p point to a member function?

Ricky
2008-01-18 00:16:29

It is an array of 1 dimensional arrays, but in order to be able to create a data structures such as that, you need to define the structure.  With an array, you tell the computer "give me an array of size 10".  With a (dynamic) matrix, you can't just say "give me a matrix of size 5x4".  This is because the memory itself is shaped like an array, but it's not shaped like a matrix.  This makes creating arrays more natural than doing so with a matrix.

mikau
2008-01-17 15:49:02

whoops! i thought i replied to this.

i meant to say a 2 dimensional array in C++ is not an array of 1 dimensional arrays in C++, instead it seems to be stored and handled differently.

Am I not correct?

Ricky
2008-01-16 11:54:37

i agree a matrix is like an array of arrays, but C++ does not seem to agree in terms of syntax.

?

A matrix is an array of arrays, but this view is naive because you can't just say to your computer, "Make me an array of arrays".  You need to define more structure behind it.  OO programming allows you to do this very naturally:

vector<vector<int> > matrix;

Board footer

Powered by FluxBB