How did I break inheritance? - c++

Refactored from bug_report_view.cc and bug_report_view.h, I extracted send_report(), report_phishing(), a few other smaller functions and BugReport::Cleanup into bug_report.cc and bug_report.h (my versions). Compiling now, I get:
[...]bug_report.cc:196: error: no matching function for call to ‘URLFetcher::URLFetcher(std::wstring&, URLFetcher::RequestType, BugReport::PostCleanup*)’
../chrome/browser/net/url_fetcher.h:136:
note: candidates are: URLFetcher::URLFetcher(const URLFetcher&)
../chrome/browser/net/url_fetcher.h:82:
note: URLFetcher::URLFetcher(const GURL&, URLFetcher::RequestType, URLFetcher::Delegate*)
For some reason, BugReport::PostCleanup (in my version) isn't recognized as a subclass of URLFetcher::Delegate, but BugReportView::PostCleanup (in the first links) is. So where did I mess up? Thanks.

The problem is not the type of the PostCleanup class. The problem is the type of the first parameter to the URLFetcher class constructor. The constructor expects a GURL &, you are passing a std::wstring called post_url. You will need to perform some kind of conversion between the two. Possibly something like this would be appropriate:
GURL post_url(l10n_util::GetString(IDS_BUGREPORT_POST_URL));
URLFetcher* fetcher = new URLFetcher(post_url, URLFetcher::POST,
new BugReport::PostCleanup);
In the code you have modified, the class has a GURL member which is initialised in the constructor, you have changed it to a variable referenced only in that one function, but changed the type.

At:
URLFetcher* fetcher = new URLFetcher(post_url, URLFetcher::POST,
new BugReport::PostCleanup);
it can't find an URLFetcher constructor thzat takes the parameters youn give it - the problem is presumably in url_fetcher.h, which you haven't shown.
BTW, there are a lot of other problems and bad practices exhibited in your code - it would be a good idea to instigate a full code review ASAP.

First version used member variable post_url_ second just local variable post_url.
Please describe what is GURL type - it is typedef on std::wstring or something other.

Related

How to create pass-by-ref parameter in BlueprintCallable UFUNCTION that doesn't need variable plugged to pin and checks if variable was plugged or no?

What I want to achieve:
A Blueprint Callable function that takes UPARAM(ref) bool& as a parameter which can but doesn't have to be passed inside of Blueprint Graph (variable doesn't have to be plugged into input pin to this node for the graph to compile). And by *asing on if the parameter has been passed or not, the function will behave in a slightly different way.
For example, I was able to create something like this in C++ (pastebin imgur):
void Func(bool& param = *(bool*)0)
{
if (&param == nullptr)
// do something
}
Above code compiles and consistently behaves in VS2022, allows to call Func() without passing any parameters in it and execute code basing on if the parameter has been passed which is the exact behavior that I'm looking for.
However, this code is a C++ undefined behaviour not allowed for UFUNCTIONs. So, when I try to declare something similar as BlueprintCallable UFUNCTION in Unreal, this of course will not compile:
UFUNCTION(BlueprintCallable)
static void Func(UPARAM(ref) bool& param = *(bool*)nullptr);
due to error:
C++ Default parameter not parsed: param "(bool)nullptr"
So my question is:
Is the "behaviour/functionality" that I want to achieve even possible in BlueprintCallable functions?
Is there any workaround to what I've described above? For example in form of macros, custom data containers or function specifiers?
I think summary of my question might be a little bit misleading. I just want to recreate this type of code/behaviour pastebin imgur in Unreal's 'UFUNCTION(BlueprintCallable)'. Yes, I understand that given example is an UB, but this is the closest result to what I want to create. This is just an example.
This question is Unreal Engine / UFUNCTION related. This is not a typical C++ issue. Unreal uses macros for UFUNCTION declaration and compiles in a different way than regular C++ (UFunctions). Because of that pointer cannot be used as parameter in this case as Unreal does not allow it. However pointer would be an actual solution to this question if it were only about pure C++.
Possible but not exact solutions:
meta = AutoCreateRefTerm( "param" ) can be specified in the UFUNCTION declaration. This allows Blueprint Node to have default value in pass-by-ref pin. However, with this approach another condition (bool pin) is needed because it is not possible check if actual variable gets passed or not.
In comments TOptional has been mentioned. This data container is actually something that exactly fits here, but TOptional cannot be used as parameter in BlueprintCallable UFUNCTION due to "Error: Unrecognized type 'TOptional' - type must be a UCLASS, USTRUCT, UENUM, or global delegate.", or at least I don't know how to use it.
My question has been closed as a dupe of Is null reference possible? which isn't true. My question asks for high level functionality in Unreal's Blueprints/UFUNCTIONS that would omit the need of "null reference". Something like TOptional::IsSet

Creating a temporary nameless class instance in C++

Sometimes I need to instantiate a class only to call its one methed. For example to read registry in Qt.
QSettings settings;
settings.setValue( DEST_FOLDER, destDir );
But I use this syntax instead.
QSettings().setValue( DEST_FOLDER, destDir );
It works but I haven't seen any documentation on it, what is it called? I searched 'nameless c++ class' but it doesn't come up. There is anonymous class but I think that's different.
My question is its a little like calling the constructor..is there a name to this kind of syntax? Can someone explain or point me to documentation to this kind of initialization? Dumb question but is it bad to do this?
QSettings().setValue( DEST_FOLDER, destDir );
will give you a default constructed temporary instance of QSettings that will exist until the end of the full expression, in this case the ; at the end of the line, and then call setValue(...) on said temporary.
You can call every constructor you want this way, not just the default constuctor as you did in your example. If e.g. class A has a constructor and a member function fun that take a single int,
A(5).fun(20);
will create a temporary object of type A via A::int(5) and then call A::fun(int i) with i = 20 on it.
This is allowed and safe, whether this actually makes sense depends on what the method you call does in the end.
Note that the class itself is neither nameless nor temporary, only the instance you create is.
"My question is its a little like calling the constructor..is there a name to this kind of syntax?"
Yes, it's called a creating a temporary rvalue
"Can someone explain or point me to documentation to this kind of initialization?"
It's a basic c++ feature, documentation may be given in the C++ standard specification.
"Dumb question but is it bad to do this?"
Totally depends on what features QSettings actually provides, and these kind of calls persist the settings, as you want to do.

C++: use of “().” and “()[].”

I am trying to understand the programming of Siemens scanner using C++ and given that my C++ skills are limited, I am having problems in understanding many parts of the code provided by the vendor.
Problem 1
For instance, the code uses reference (rMrProt) to object MrProt and notations (such as the use of use of (). and ()[].) are very confusing to me.
For instance:
ImageSamples = rMrProt.kSpace().baseResolution()
ImageSize = rMrProt.sliceSeries()[0].readoutFOV()
Some explanation of these statements would be appreciated.
All information regarding object MrProt are in “MrProt.h”, “MrProt.dll”, “MrProt.lib”. All these files have been shared at:
https://docs.google.com/open?id=0B0Ah9soYnrlIYWZkNDU2M2EtYTNmNC00YTc5LTllMzItYzIyMWU4M2ZhY2Fi
Problem 2
Also, I have been trying to read MrProt.dll and MrProt.lib without any success. Only now, I came to know of dumpbin. Any help would be appreciated.
Problem 3
Another confusion that I have is related to some part of MrProt.h itself. There is a statement in MrProt.h:
class __IMP_EXP MrProt: public MrProtocolData::MrProtDataDelegate
{
typedef MrProtocolData::MrProtDataDelegate BasicImplementation;
public:
MrProt();
MrProt(const MrProt& rSource);
…
….
}
Here, __IMP_EXP, I guess that it’s some compiler specific stuff.. some decoration etc. But, I still have no idea what to make of this.
Problem 1.
rMrProt.sliceSeries()[0].readoutFOV()
means
Take rMrProt's sliceSeries member and call that. Apparently, it returns an array-like object, something that can be indexed.
From the result, take the first element ([0]). That's some kind of object.
On that element/object, call readoutFOV.
Problem 2. You're not really supposed to read binary files. There should be documentation with them.
1)
ImageSamples = rMrProt.kSpace().baseResolution()
This is just method chaining. You call the method kSpace() on rMrPrto which returns an object, and you call baseResolution() on that object.
2) Those are binary files. What would you expect to see? To read them you'd have to be an expert in asm or at least know some low-level concepts.
3) __IMP_EXP is a common type of directive that tells the compiler that the class is either exported or imported.
It expands to _declspec(dllimport) or _declspec(dllexport), depending on whether the definition of the class is in the current module or another module.
identifier() is a method/function call
identifier[i] returns the i'th element in an array.
identifier()[i] returns the i'th element of the array returned by identifier()
I can only help on problem 1:
if the return value of rMrProt.kSpace() is a struct. instead of saving it to a struct and then access it's member you can directly access a member of his with rMrProt.kSpace().MemberName
same for rMrProt.sliceSeries() which I guess is returning an array. so rMrProt.sliceSeries()[0] will access the first value in the returning array.

quickfix: how to convert FIX::Account to a c_string or other types

I am using quickfixengine to build a FIX application. quickfix almost has no documentation, pretty much like a blackhole. I have a variable which is a FIX::Account type. I know it is a FIX string type, but how do I get the string out (to be a c-string). I tried something like this, it does not pass the compilation.
FIX::Account acct;
// populate acct somewhere else
printf(acct.c_str());
Compiler error is error: ‘class FIX::Account’ has no member named ‘c_str’
basically, I'd like to know how to get to know the constructor, interface of every FIX types? which files contain these?
thank you
I assume you're using the C++ API. In this case, all fields inherit from FIX::FieldBase which has a convenient getString() method (see here).
I agree that is not easy to understand at a glance the declarations of FIX fields. That's because all classes related to FIX messages are directly generated from the XML specifications of the FIX protocol.
getValue() method should be used for getting string fields.
printf(acct.getValue().c_str());
Hope this helps.

C++ Detecting an implicit cast of 0 to a class

I am calling a function in a API that returns a Answer object. Under some conditions, the API will return EMPTY_answer, which is defined as such:
#define EMPTY_answer ((Answer)0)
of course, attempting to access a Answer variable or function from an EMPTY_answer object crashes the application.
Trying to test for it using if(lAnswer == EMPTY_answer) also crashes the application. Is there any way to detect if the API is returning EMPTY_answer?
Edit:
I didn't code the api and I can't modify it in any way, I'm just digging through .h files trying to figure this out. And yes, I am aware that casting 0 to a class is a bit too creative to put it mildly. I just noticed that the == operator is overridden
(...)
class ExportedByJS0CORBA Answer
{
(...)
int __stdcall operator==(Answer *ipt) const;
}
the function being called is
static SubClassOfAction Query();
I'm simplifying names and quite a few layers of inheritance
To be more precise, it crashes with a Segmentation Fault.
Instead of doing a very ugly cast which is almost guaranteed to trigger undefined behavior, just make a static global variable which is used as the "empty" answer. You don't need to use this object in any way, just make it exist so it can be used as a sentinel.
In Answer.h:
extern const Answer EMPTY_answer;
In Answer.cpp:
const Answer EMPTY_answer; // use any constructor parameters that will be appropriate
If Answer is a class type, as the text of your questions suggest, then (Answer) 0 will construct a temporary Answer object using the constructor that accepts 0 as an argument (apparently such constructor exists). In this case attempting to access the members of that object will not crash anything, unless Answer class is specifically implemented to crash in this case (intentionally or unintentionally). So your "Of course..." claim makes no sense whatsoever. There's no immediate reason for such code to crash.
If you observe crashed in someAnswer == EMPTY_answer comparison, that would either mean that the implementation of == operator is buggy, or that either the LHS or the RHS are not valid objects. For example, it might turn out that it is illegal (by design) to construct an Answer object by conversion from 0. If so, then you should simply stop using (Answer) 0 in your code and find another, correctly supported object value to indicate an empty answer.
your original method of just checking for EMPTY_answer is the right way to solve this. Your real problem is why that crashes. What type is lAnswer? Or Answer for that matter... you can't cast 0 to a class like that.