0

Very simple C++ question. This is the Aspose slides C++ library.

SharedPtr<MemoryStream> stream = MakeObject<MemoryStream>();

The line above works. But how can I make stream const?

const SharedPtr<MemoryStream> stream = MakeObject<MemoryStream>();

This just returns SharedPtr. How can I make it "const SharedPtr"?

yes I'm not super experienced with C++..

EDIT:

Some more info:

So I need const because I need to pass the result to a function which expects const SharedPtr<T> as argument.

void    Save (const SharedPtr< System::IO::Stream > &stream, const Imaging::ImageFormatPtr &format)

The problems is:

const SharedPtr<MemoryStream> stream = MakeObject<MemoryStream>();

does not return const SharedPtr<T>, it just returns SharedPtr<T> for some reason. That's the strange thing.

This is the relevant segment from my code:

    const ImageFormatPtr png = System::Drawing::Imaging::ImageFormat::get_Png();
    const ImageFormatPtr jpeg = System::Drawing::Imaging::ImageFormat::get_Jpeg();
    
    if (format != "png" && format != "jpeg")
    {
        throw Php::Exception("Invalid format: " + format);
    }

    try
    {
        SharedPtr<System::Drawing::Bitmap> bmp = _slide->GetThumbnail(scaleX, scaleY);
// This is not const!
        const SharedPtr<MemoryStream> stream = MakeObject<MemoryStream>(); 

// ... stream is supposed to be const here
        bmp->Save(stream, format == "png" ? png : jpeg);

Thanks in advance!

user2297996
  • 1,382
  • 3
  • 18
  • 28
  • When you say `this just returns SharedPtr`, what do you mean by `this`? Do you mind adding some context? – alter_igel Mar 02 '21 at 21:51
  • What the point ? Why do you need it const ? What do you what to achieve, a read only stream ? – Victor Gubin Mar 02 '21 at 21:54
  • It appears you have already succeeded in making a `SharedPtr` into a `const SharedPtr`. Do you want the `T` to be const instead? Are you wondering why a non-const `SharedPtr` can be constructed from a `const SharedPtr`? Again, some more context would help. – alter_igel Mar 02 '21 at 21:56
  • Thanks for edit. Are you receiving any kind of compiler error? You appear to have a very basic misunderstanding about what `const` means and also about how references-to-`const` work in C++. I would suggest you put this aside and spend some time with a good [C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) which will clarify this for you. From your current question, it sounds like you've gotten very far ahead of yourself. – alter_igel Mar 02 '21 at 22:53
  • But to answer briefly, there doesn't appear to be anything wrong with your code. Your `SharedPtr` doesn't need to be `const` at all because a function accepting `const SharedPtr&` (carefully note the `&`) will simply receive a read-only view of that `SharedPtr` that you pass to it. This is very fundamental to C++. Unless you have a compiler error or some other definitive problem with this code, I'm afraid this simply sounds like a basic misunderstanding of the language. – alter_igel Mar 02 '21 at 22:55
  • OK thanks for the info. Actually I'm getting a compiler error. Its says there is no matching function to call for Save(SharedPtr, const ImageFormatPtr&). This is happening because the first argument is not const. Because otherwise it would match the signature. No? In the previous version of this library, the Save() function didn't expect const arguments and this code worked fine (without const). I'm trying to upgrade to the latest Aspose.Slides and now the signature is different (see above) and it doesn't work anymore. – user2297996 Mar 02 '21 at 23:20
  • Please include the exact error message in your question. Additionally, if you could create a [mcve] of ~30 lines, it would be much easier to figure out what's wrong. – alter_igel Mar 03 '21 at 19:37
  • Assuming you're trying to call [`System::Drawing::Image::Save(const SharedPtr&, const Imaging::ImageFormatPtr&)`](https://apireference.aspose.com/page/cpp/class/system.drawing.image#ab8f52c4783883507d21e63c706d27d35), and given that `System::IO::MemoryStream` inherits from `System::IO::Stream`, I don't see what the problem is. `SharedPtr` should be implicitly convertible to `SharedPtr` if I understand correctly. – alter_igel Mar 03 '21 at 19:41
  • Can you check whether the following line compiles for you, if you place it before the `save` call and comment out the `save` call? `SharedPtr x = stream;` – alter_igel Mar 03 '21 at 19:42
  • Yes that compiles. Thanks for the help and info. The problem is solved now, a .h file (memory stream) was missing. I didn't have include that explicitly before. But the error message was really misleading and gave no real hint. – user2297996 Mar 04 '21 at 18:19

0 Answers0