-2

Let say I've decleared this within MyTools.h

#ifndef _MYTOOLS_
#define _MYTOOLS_

typedef struct {
    // const
    double LN20;
    double LN40;

    // methods
    double NoteToFrequency(int noteNumber);
} Tool;

extern const Tool tool;

#endif // !_MYTOOLS_

For every compilation unit, there is only a global/const/unique instance of Tool. Exactly what I want.

But now: how can I define it? In the .h i've only declared it. How can I define it in .cpp? Tried somethings like:

tool.LN20 = 1.34;

But of course it doesn't works. And the method's definition?

markzzz
  • 47,390
  • 120
  • 299
  • 507
  • 1
    related/dupe: http://stackoverflow.com/questions/20716091/global-const-object-shared-between-compilation-units – NathanOliver Dec 15 '16 at 15:43
  • 2
    `const Tool tool {1.34, 3.14};`? Working example: [here](https://ideone.com/xiiJ2e). – Algirdas Preidžius Dec 15 '16 at 15:44
  • 1
    Those members that you have labeled `// const` are *not* actually constant, by the way. – Cody Gray - on strike Dec 15 '16 at 15:45
  • @AlgirdasPreidžius: but how can I set it accessing to the name? If I have 40 variables would be hard to define it. And the method? How would you define it? – markzzz Dec 15 '16 at 15:47
  • 2
    Don't write C, write C++: `struct Tool { double LN20; ... };`. You might want to take a look at [this list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Dec 15 '16 at 15:53
  • @paizza You cannot. You need to define it, before you can even access any properties of it (note, variable declarations with `extern` are only that - declarations), and definition, of a variable, is of form ` [];`, where initialization is optional. And since you want your variable to be `const` - the single instance where you can _set) values to member-variables is during the definition. In my opinion, you should read a book about C++, to better grasp the basics of C++, before you do any coding. – Algirdas Preidžius Dec 15 '16 at 15:55
  • 1
    Identifiers starting with underscore and a capital letter - like `_MYTOOLS_` - are reserved for the compiler's implementation, by the way. Don't use them. – molbdnilo Dec 15 '16 at 15:56
  • @AlgirdasPreidžius: I mean "how I define it" accessing by its properties on the .cpp file. – markzzz Dec 15 '16 at 15:58
  • @paizza Did you read my comment, at all? Better stick to reading books, until you grasp the basics. – Algirdas Preidžius Dec 15 '16 at 16:09
  • Of course I did, but I think you misunderstand what I mean! Of course I need to define it before access to any properties. I'm not talking about this... – markzzz Dec 15 '16 at 16:10
  • As @NathanOliver suggested check http://stackoverflow.com/questions/20716091/global-const-object-shared-between-compilation-units?noredirect=1&lq=1 – A.N Dec 15 '16 at 16:12
  • @A.N: it exactly what I want to do...but...did you read my question? :) I'm asking how to define it in the .cpp. I.e. How can I set in the .cpp tool.LN20 = 3.14 and write the definition of NoteToFrequency method? – markzzz Dec 15 '16 at 16:17
  • check my answer for updated code. – A.N Dec 15 '16 at 16:26
  • @CodyGray: if I declare the struct `const`, I can't edit them (neither directly nor getter/setter). So why you said they are not `const`? http://coliru.stacked-crooked.com/a/4177ff1665db488f – markzzz Dec 16 '16 at 09:07
  • The *instance* is `const`, which of course means that you cannot change its members. But that doesn't make the members *themselves* `const`. I could easily declare a new `ToolTest` object, and then I could manipulate its members. – Cody Gray - on strike Dec 16 '16 at 09:40
  • @CodyGray, yeah of course! That's why I declared it non-const! So I can decide prior on declaration. – markzzz Dec 16 '16 at 10:19

1 Answers1

-1

extern doesn't define any variable it just declares it. What you wan't to achieve can be done as below:

The link Global const object shared between compilation units explains how to do it with extern const

t.h file

#ifndef _MYTOOLS_
#define _MYTOOLS_

struct Tool {
    // const
    double LN20;
    double LN40;
    double NoteToFrequency(int noteNumber);

} ;

extern const Tool tool ;

#endif // !_MYTOOLS_

t1.cpp

#include "t.h"
#include <stdio.h>

void use_tool()
{
    printf("%f\n",tool.LN20);
    printf("%f\n",tool.LN40);
    return;
}

t2.cpp

#include "t.h"
#include <stdio.h>


const Tool tool = {.LN20 = 20.0, .LN40 = 30.2};
double Tool::NoteToFrequency(int noteNumber)
{
    return 12.0;
}
void use1_tool()
{
    printf("%f\n",tool.LN20);
    printf("%f\n",tool.LN40);
    return;
}
int main()
{
    void use_tool();
    use_tool();
    use1_tool();
    return 0;
}

Hope this helps.

Community
  • 1
  • 1
A.N
  • 541
  • 2
  • 13