-16

I'm trying to assign a variable or attribute of an object in C++ and am unsure as to how to do this.

class ClassA{
public:
    double a,b,c,d,e,f;
};

ClassA * example = new ClassA;
example.a =.01; 

Why do I need to include the '*' in defining the object 'example' (I got this through another tutorial). How do I correct this to correctly set the values of a, b, c, d, e, f for the object 'example'

bgporter
  • 35,114
  • 8
  • 59
  • 65
user3681670
  • 105
  • 1
  • 2
  • 9
  • 4
    Please read a better `C++` tutorial. You seem to be missing the absolute basics of this language. – gexicide Jun 17 '14 at 14:52
  • 1
    What are you trying to assign the variables to? The * is the dereferencing operator, which means you're creating a variable which points to the location in memory where the object you want exists: http://www.cplusplus.com/doc/tutorial/pointers/ You might want to start with the www.cplusplus.com site and work through some of the tutorials there. – Maurice Reeves Jun 17 '14 at 15:01
  • @MauriceReeves There is no de-reference operator in OP's code. I worry you might be confusing them more than they already are :-) – juanchopanza Jun 17 '14 at 15:18
  • LOL. Yeah probably. Was trying to get the terminology right, but might have muddied the waters further. – Maurice Reeves Jun 17 '14 at 15:56

1 Answers1

1

Try this.

ClassA example;
example.a =.01; 

You were using pointers in your code, and it looks like you need practice with other ideas before starting with pointers.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • 1
    Or `ClassA example = {0.01};` – juanchopanza Jun 17 '14 at 14:53
  • It says unknown type name 'example' for Drew's comment – user3681670 Jun 17 '14 at 14:54
  • hmm...it is still saying unknown type name 'example' – user3681670 Jun 17 '14 at 14:58
  • 1
    @user3681670 You need to put the code in this answer inside of a function. Statements cannot go outside of functions. – juanchopanza Jun 17 '14 at 15:00
  • 1
    @user3681670 I think you're being cheated by the awful tutorial you found. Find a new tutorial. We can't tell you why your mystery tutorial is doing such a poor job. – Drew Dormann Jun 17 '14 at 15:04
  • Ok thanks. I see your response worked. Can you advise on what the following would mean: if I'm not going to use pointers (and hence its not truly OOP), then I can just use C instead of C++ – user3681670 Jun 17 '14 at 15:06
  • 1
    @user3681670 I'm glad it worked! Yes, I can advise - that's a bunch of nonsense. Where are you getting this awful information? – Drew Dormann Jun 17 '14 at 15:07
  • Someone I was working with made the comment (I don' think they were trying to be negative per se). Anyway, given how popular this question was I may need to focus on the basics more lol – user3681670 Jun 17 '14 at 15:09
  • @user3681670 The only reason why pointers would be better is for inheritance purposes, but even then I would recommend references. – yizzlez Jun 17 '14 at 15:09
  • @user3681670 as someone already commented, [you should take a look here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). And then share that link with your coworkers. I don't think your coworker was being negative either, but that coworker is **feeding you some very bad information**. – Drew Dormann Jun 17 '14 at 15:10
  • 1
    @user3681670 It should be the other way around. If you're going to use pointers everywhere you might as well use C instead of C++. But it sounds like you're getting advice from people without a clue. – juanchopanza Jun 17 '14 at 15:11