Logical fallacy questions!

Dec 13, 2015 at 5:19am
I'm studying an old test and I was wondering if my answers to these are correct. I posted a screenshot if it below


i. Not sure about this one. I'd guess 1.
ii. 2
iii. this->x, 3.
iv. catch

b) i. Is it because you can't set f to a value directly like that? Pretty sure you can't, or is it something to do with the const/missing bracket behind bar?

Not really 100% sure on any of them and #1 I'm really unsure about. If anyone could help me it would be great!

http://imgur.com/6VUkYzj
Dec 13, 2015 at 5:39am
1)
http://stackoverflow.com/questions/751681/meaning-of-const-last-in-a-c-method-declaration
https://msdn.microsoft.com/en-us/library/07x6b05d.aspx - go to 'const member functions'

2)
http://www.cplusplus.com/doc/tutorial/pointers/ - go to 'Address-of operator'

3)
Yes, it is catch

4) Left operand is of type 'Foo', right operand is of type 'int'. There is no conversion given for 'int' to 'Foo'.
Dec 13, 2015 at 5:43am
Thanks I'll read those. For 4), can't you set a Foo value equal to an int though, since one of the members is an int? Or do you need to do it like Foo x; f.function=22;?


Also it asked what's wrong with this down the page, but I'm not sure if it's wrong because average has no type, or if because values is unintalized, or something else entirely.


average(const std::vector<double>& values);
Dec 13, 2015 at 6:37am
That has to be a function declaration.

There is no return type specified. The const reference to the vector is OK. Iterate through it using a const_iterator.

Look at me. I thought that 'What is invalid with each of the following' was the same problem.

Top one: missing ';' after bar

Bottom one: No way to assign '42' to type 'Foo', at least not from what I can see.
Unless there was a constructor that took an int as a parameter.
Dec 14, 2015 at 5:34pm
So for one, is c correct since it's taking a const reference? And for the part b 1), the ; is missing but my compiler won't run the code either way. Is the structure of saying const Foo f=1; not valid?
Dec 14, 2015 at 9:04pm
The statement 'const Foo f = 1' is invalid because there is no conversion given from 'int' to 'Foo'. They're two different types and the compiler doesn't know how to convert from an 'int' to a 'Foo'. You could put in a constructor that took an 'int' or an '=' operator overload to do the conversion.

Look here for the operator overload:
See: http://en.cppreference.com/w/cpp/language/operators

Look here for the constructor that takes an 'int', also called a "converting constructor", which can be used to do 'Foo f = 1'.
Also see: http://en.cppreference.com/w/cpp/language/converting_constructor

Technically, you can pass a const reference to a vector, but you cannot iterate through it normally. You must use a 'const_iterator' to do that.

Look here for a small verification on passing a vector by const reference:
See: http://www.cplusplus.com/forum/general/39639/
Last edited on Dec 14, 2015 at 9:05pm
Topic archived. No new replies allowed.