-3

In below scenario for class xyz I am facing some issue when I call method2 and keep breakpoint to called place at method1 its jumping to the middle method1 having the extra std::optional argument

class xyz {
public:
    virtual int method(int, int) = 0;
    virtual int method1(int32, int32, struct abc) = 0;
    virtual int method1(int32, int32, struct abc, std::optional<int32> l) = 0;
    virtual int method2(int32) = 0;
    virtual int method3(int, int) = 0;
    virtual int method4(int, int) = 0;
};

class x : public xyz {
        int method(int, int);
        int method1(int32, int32, struct abc);
        int method1(int32, int32, struct abc, std::optional<int32> l);
        int method2(int32);
        int method3(int, int);
        int method4(int, int);
};


obj1->method2(10); // obj1 is pointer object of class x which implements method of interface class xyz

Initially I did not able to identify the exact cause but when I moved this method1 both of the pure virtual functions up or down somewhere after method3 or method4 then calling method3 or 4 calls to middle method1 having the extra std::optional argument (same behavior seen for method2 as explained above), by seeing this behavior I am not sure about the inside concept why such behavior, please explain me whats wrong with the code and how do I avoid?

Is it something because of std::optional variable or something else? also I did not find any examples or query with similar behavior.

Updated my query for better understanding, cant provide more info/code as code is proprietary and tried to shorten problem statement out of it.

For debugging I am using Visual Studio and kept break point at those called functions.

user2621476
  • 93
  • 1
  • 8
  • 1
    How you call `method2`? How do you define a breakpoint, using which debugger? How do you run your app? What is your stacktrace on breakpoint and how does it differ from expected behaviour? – pptaszni Aug 30 '23 at 14:04
  • Please read [ask] with a [mcve]. – Richard Critten Aug 30 '23 at 14:04
  • Something somewhere else in your code has undefined behaviour. It has nothing to do with `optional`. It is possible that you have just forgotten to compile your derived classes or any uses of this class. – molbdnilo Aug 30 '23 at 14:10
  • This question's shown code fails to meet Stackoverflow's requirements for showing a [mre]. Because of that it's unlikely that anyone here can conclusively answer the question; but only guess at the most. You need to [edit] your question to show a minimal example, no more than one or two pages of code (the "minimal" part), that everyone else can cut/paste ***exactly as shown***, compile, run, and reproduce the described issue (the "reproducible" part, this includes any ancillary information, like any input to the program). See [ask] for more information. – Sam Varshavchik Aug 30 '23 at 14:12
  • You should have UB. Vtable seems mexed up, so probably a call from destructed object, or a virtual (actually not ;-) ) call from a constructor/destructor. – Jarod42 Aug 30 '23 at 14:20
  • Why (and how) are you deliberately calling pure virtual functions? – molbdnilo Aug 30 '23 at 14:21
  • Just make sure your constructor is proteced, you should never hav an instance of that class. Do you somehow somewhere use a `static_cast` or any other cast? If so your design is broken – Pepijn Kramer Aug 30 '23 at 14:27
  • @PepijnKramer I have not used static_cast, I have one class which implements all pure virtual methods of above Interface class and using that class object methods will be called, when I tried keeping one pure virtual method it just works so something with similar virtual methods which I have written. – user2621476 Aug 30 '23 at 14:42
  • Did you call any of the methods in a constructor or destructor? Or do you accidentally have a dangling pointer to a deleted instance? Oh and have you attached a debugger and checked that `obj1` still points to a valid object? Also ALL of your methods in your class `x` should look like this : `int method2(int32) override;` (See [rule 128](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c128-virtual-functions-should-specify-exactly-one-of-virtual-override-or-final) of the C++ core guidelines) – Pepijn Kramer Aug 30 '23 at 14:54
  • Instead of slowlly leaking out details in response to comments, post the smallest code example that you can come up with the **compiles**, **runs**, and shows the problem. – Pete Becker Aug 30 '23 at 15:56

0 Answers0